okay im trying to make a sort of data base, i dont really need it to store the data permanently, rather just in memory for the time being..
so i thought that what i needed were objects that would be able to link in a link list with each other and hold different data types... i wanted to use a virtual function in a parent class that all others would implement in their own way, but when i try to use vurtual functions my code implodes.. any way i guess i should post the code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//MushData.h
//this class isnt meant to be used,
//other classes that will be used inherate from it
#ifndef _MUSHDATA_H
#define _MUSHDATA_H
namespace MushBase{
class MushData{
public:
MushData* prev;
MushData* next;
//pub vars
MushData();//no args
MushData(MushData* p,MushData* n);//pointers for prev and next
virtualvoid setData();
};
}
#endif//_MUSHDATA_H
In MushData, function is 'void setData()' whereas in MushString function is 'void setData(char*)'
In C++ two functions with the same name, but different parameters are considered different.
Add a 'char*' parameter to 'virtual void MushBase::MushData::setData()'
The prototype of the setData() and the definition don't match:
1 2 3 4 5 6 7 8 9 10 11 12
//MushData.h
//this class isnt meant to be used,
//other classes that will be used inherate from it
#ifndef _MUSHDATA_H
#define _MUSHDATA_H
namespace MushBase{
class MushData{
...
virtualvoid setData();
};
}
#endif//_MUSHDATA_H
whats a "pure" virtual function, and no i dont plan on using the MushData class for anything other than to make all the other ones line up in a linked list...
how can i make it so that i can set the data, when i may not know how future children classes will need to be set...
Pure virtual function is a function of a base class which don't contain any instructions. It only make an appeal that in the derived classes have to be made one.
i use code::blocks, http://www.codeblocks.org/
it was recomended by some tutorial long ago, i like it because its simple, tho i'd switch to one thats better as long as it isnt to fancy and compicated...
#include <iostream>
usingnamespace std;
class base
{
public:
virtualvoid fnc();
};
void base::fnc(){cout << "base " << endl;}
class derived:base
{
public:
void fnc (){cout << "derived " << endl;}
};
int main()
{
base * test1 = new base;
base * test2 = (base*)new derived;
test1->fnc();
test2->fnc();
return 0;
}
I think that you should take out the setData function from your base class. The specializated setData function belong to only the derived classes in my opinion:
class MushBase
{
};
class MushString: public MushBase
{
void setData(char *s);
};
class MushBeer: public MushBase
{
void setData(int amount);
};
#include <iostream>
usingnamespace std;
class base{
//just to make a Linked List possible
};
class derived : public base{
public:
int i;
int getIt(int j);
};
int derived::getIt(int j){
return i+j;
}
int main(){
//i need results like:
derived var;
var.i=5;
cout<<var.getIt(3);
// that, but it needs to work from a pointer like
base* var2;
var2 = new derived;
//var2->i=5;
//var2->getIt(4);
//^ wont work
cin.get();
}
There is no point in having a base class with no member functions or data members...you would have to define getIt() as a virtual function inside the base class.
thanl you helios, i didnt even think of useing a template!
i can use that data to store pointers as well? i need a way to store strings so if i cant store a pointer than im kinda back at square one :(
ok, you can store the address of derived classes with the help of pointer of base class, but if you want to use the function of a derived class you should convert that pointer to pointer of derived class.
1 2 3 4 5 6 7 8 9 10
base* var2;
var2 = new derived;
//var2->i=5;
//var2->getIt(4);
//^ wont work
// just make a pointer of derived class
derived * tmp = (derived *)var2;
// and you can use the specialized functions
tmp->getIt(4);
#include <iostream>
usingnamespace std;
class base
{
public:
virtualvoid fnc();
base *next;
};
void base::fnc(){cout << "base " << endl;}
class derived:base
{
public:
void fnc (){cout << "derived " << endl;}
void fnc (int i){cout << "derived " << i << endl;}
};
int main()
{
base * test1 = new base;
base * test2 = (base*)new derived();
test1->fnc();
test2->fnc();
derived *tmp = (derived *)test2;
tmp->fnc(4);
return 0;
}
The output:
base
derived
derived 4
It is good for that you want to store different classes which is derived from a base class. Then you can make a list which is based on basic class and put into your different classes into the list. But if you want to use them, then you have to convert to the suitable class.