An example of void pointer
#include <iostream>
using namespace std;
class temp
{
int num;
public:
temp(){num=10;}
void * getNum(void){return ((void *) &num);}
void setNum(void * myNum){int * thisNum=(int *) myNum; num=*thisNum;}
};
int main()
{
temp A;
int mainnum= *(int *) A.getNum();
int * n;
int i=30;
n= &i;
A.setNum((void *) n);
mainnum= *(int *) A.getNum();
return 0;
}
Chen's Cplusplus blog
Saturday, December 18, 2010
Thursday, November 25, 2010
Oh, yeah, linked list!
After discussion, the text file is using linked list again.The example demonstrates how to use Fardad's linked list. It's quite simple. He did great amount of job for us. I think dynamic array should be very simple, too. But I don't understand it. By the way, in Java, there is also a class implement linked list just similar to Fardad's code. You don't need to know how linked list builds, just use it. The following are the example. Copy it and just to run it. You will understand it.
#include "strque.h"
#include "iostream"
int main()
{
StrQue A;
//insert hello world into the end of the linked list
//and the current pointer point to "hello world"
//use visit to get the string that current points to
char str[100]="Hello, world!";
A.Append(str);
printf("%s\n",A.Visit());
//add "welcome to oop344" to the end of linked list
//current points to "welcome to oop344"
strcpy(str,"Welcome to OOP344");
A.Append(str);
printf("%s\n",A.Visit());
//go back to the head
//that means current points to "welcome to oop344"
A.GoHead();
printf("%s\n",A.Visit());
//if you know the position in the linked list, you can go directly by accessing as an array
//a.operator[](1) prints the 2nd string
//that means "welcome to oop344"
//from calling visit function, you will see that operator[] would not change the current pointers
printf("%s\n",A.operator [](1));
printf("%s\n",A.Visit());
//pirnt how many nodes in the linked list
printf("%d\n",A.Size());
//now, current points to "welcome to oop344"
A.GoTail();
strcpy(str,"The project is hard");
A.Insert(str);
//now, current points to "the project is hard"
printf("%s\n", A.Visit());
//you can the order for the whole linked list
//"hello world"->"the project is hard"->"welcome to oop344"
printf("%s\n",A.operator [](0));
printf("%s\n",A.operator [](1));
printf("%s\n",A.operator [](2));
//current points to "hello world"
//and store the current
A.GoHead();
A.StoreCur();
//now, current move to the end, "welcome to oop344"
A.GoTail();
printf("%s\n",A.Visit());
//after restore, current go back to the position you store
A.RestoreCur();
printf("%s\n",A.Visit());
return 0;
}
#include "strque.h"
#include "iostream"
int main()
{
StrQue A;
//insert hello world into the end of the linked list
//and the current pointer point to "hello world"
//use visit to get the string that current points to
char str[100]="Hello, world!";
A.Append(str);
printf("%s\n",A.Visit());
//add "welcome to oop344" to the end of linked list
//current points to "welcome to oop344"
strcpy(str,"Welcome to OOP344");
A.Append(str);
printf("%s\n",A.Visit());
//go back to the head
//that means current points to "welcome to oop344"
A.GoHead();
printf("%s\n",A.Visit());
//if you know the position in the linked list, you can go directly by accessing as an array
//a.operator[](1) prints the 2nd string
//that means "welcome to oop344"
//from calling visit function, you will see that operator[] would not change the current pointers
printf("%s\n",A.operator [](1));
printf("%s\n",A.Visit());
//pirnt how many nodes in the linked list
printf("%d\n",A.Size());
//now, current points to "welcome to oop344"
A.GoTail();
strcpy(str,"The project is hard");
A.Insert(str);
//now, current points to "the project is hard"
printf("%s\n", A.Visit());
//you can the order for the whole linked list
//"hello world"->"the project is hard"->"welcome to oop344"
printf("%s\n",A.operator [](0));
printf("%s\n",A.operator [](1));
printf("%s\n",A.operator [](2));
//current points to "hello world"
//and store the current
A.GoHead();
A.StoreCur();
//now, current move to the end, "welcome to oop344"
A.GoTail();
printf("%s\n",A.Visit());
//after restore, current go back to the position you store
A.RestoreCur();
printf("%s\n",A.Visit());
return 0;
}
Which method you choose?
In the fwtext class, you can choose one method to implement. So, which method you choose, 2D array, dynamic array or linked list?
For 2D array, it is the most easy way to start. However, it may be very hard to debug.The reason is the coding may large. For any small modification, you need to create a temp array; then copy the orginal array to temp; then modify it; and finally copy back. So, I can image there is a lot of nested looping. And typing is time-consuming. And debugging is more time-consuming.
For dynamic array, if you don't know the pointer very well. The dynamic array is the hell. I am in this situaiton.
For linked list, it is tricky because everything you need is done by Fardad's strque class. You don't need to know how it builds, updates, goes forward, goes back, inserts and deletes. You just need to use it. Once you know English, you can manage it. You don't need know the detail, you just use the function. That is also one of the strengthes of OOP.
------------------------------------------Separated line----------------------------------------------------------
But so unfortunately, our group choose linked list at first. But today we swtich to dynamic array. That killes me. I hope I can force them switch back.
For 2D array, it is the most easy way to start. However, it may be very hard to debug.The reason is the coding may large. For any small modification, you need to create a temp array; then copy the orginal array to temp; then modify it; and finally copy back. So, I can image there is a lot of nested looping. And typing is time-consuming. And debugging is more time-consuming.
For dynamic array, if you don't know the pointer very well. The dynamic array is the hell. I am in this situaiton.
For linked list, it is tricky because everything you need is done by Fardad's strque class. You don't need to know how it builds, updates, goes forward, goes back, inserts and deletes. You just need to use it. Once you know English, you can manage it. You don't need know the detail, you just use the function. That is also one of the strengthes of OOP.
------------------------------------------Separated line----------------------------------------------------------
But so unfortunately, our group choose linked list at first. But today we swtich to dynamic array. That killes me. I hope I can force them switch back.
Friday, November 19, 2010
It's a linked list
The project's border class is a linked list. Though the FWBorder * _container, it creates a list of border nodes( in future, maybe field node, dialog node etc).
Thursday, November 18, 2010
virtual function and pure virtual function
If a function has a key word "virtual", that's a virtual function. If the whole virtual function equals to zero, that's pure virtual function.
For example, virtual void f(int)-----------------------------normal virtual function, non-pure virtual function
virtual void f(int)=0--------------------------pure virtual function
If it's a pure virtual function, the base function cannot be implemented, but the derived functions must be implemented.
If it's a normal virtual function, that means it is not a pure virtual function, the derived functions may be implemented or may not be implemented. Therefore, the base functions must be implemented.(I guess. Depends on different compilers, it may give you an error message.)
For example, virtual void f(int)-----------------------------normal virtual function, non-pure virtual function
virtual void f(int)=0--------------------------pure virtual function
If it's a pure virtual function, the base function cannot be implemented, but the derived functions must be implemented.
If it's a normal virtual function, that means it is not a pure virtual function, the derived functions may be implemented or may not be implemented. Therefore, the base functions must be implemented.(I guess. Depends on different compilers, it may give you an error message.)
Thursday, November 4, 2010
va_list
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
void concat (char * des,...)
{
char* str;
va_list args;
va_start (args, des);
strcpy(des,"");
str=va_arg(args, char *);
while (str)
{
strcat(des,str);
str=va_arg(args, char *);
}
va_end(args);
//strcat(str,"\0");
//return des;
}
int main()
{
char des[120];
concat(des, "Hello", " ","I"," ","am"," ","here!",NULL);
printf("%s\n",des);
return 0;
}
#include <stdarg.h>
#include <string.h>
void concat (char * des,...)
{
char* str;
va_list args;
va_start (args, des);
strcpy(des,"");
str=va_arg(args, char *);
while (str)
{
strcat(des,str);
str=va_arg(args, char *);
}
va_end(args);
//strcat(str,"\0");
//return des;
}
int main()
{
char des[120];
concat(des, "Hello", " ","I"," ","am"," ","here!",NULL);
printf("%s\n",des);
return 0;
}
Tuesday, October 19, 2010
Subscribe to:
Posts (Atom)