Derived class & Function pointer

M

mast4as

Hi everyone

I have a problem that many people probably came across before but I
didn't find an answer on the net yet ...

I have a base class Bass and 2 derived class DerivedA & DerivedB. I
can create an new instance of the DeriveA class but then need to call
a method of the DerivedB class to save the data of the base class into
a specific file format. So I thought of using function pointer but can
seem to figure it out:

class Base
{
public:
float *data;
Base() { data = new float[10]; }
~Base() { delete [] data; }
virtual void SaveFormat() = 0;
}

class DerivedA : public Base
{
public:
void SaveFormat() { // save data in file format A }
}

class DerivedB : public Base
{
public:
void SaveFormat() { // save data in file format B }
}

int main()
{
Base *derivedA = new Derived A;
// now I want to save the data hold in derivedA but by using
SaveFormat from DerivedB ???
????
// this is where I am lost... I tried
void (Base::*SaveFormatPtrFunc)() = &DerivedB::SaveFormat;
derivedA->*SaveFormatPtrFunc();
// but that doesn't compile ;-(
}

Does anybody know what's the best way of doing this ?

Thank you -coralie
 
V

Victor Bazarov

mast4as said:
I have a problem that many people probably came across before but I
didn't find an answer on the net yet ...

I have a base class Bass and 2 derived class DerivedA & DerivedB. I
can create an new instance of the DeriveA class but then need to call
a method of the DerivedB class to save the data of the base class into
a specific file format.

Doesn't sound right. Sounds like a design pulled by the ears to do
something that isn't really its purpose.
> So I thought of using function pointer but can
seem to figure it out:

class Base
{
public:
float *data;
Base() { data = new float[10]; }
~Base() { delete [] data; }
virtual void SaveFormat() = 0;
} ;

class DerivedA : public Base
{
public:
void SaveFormat() { // save data in file format A } }
} ;

class DerivedB : public Base
{
public:
void SaveFormat() { // save data in file format B } }
} ;

int main()
{
Base *derivedA = new Derived A;
// now I want to save the data hold in derivedA but by using
SaveFormat from DerivedB ???
????
// this is where I am lost... I tried
void (Base::*SaveFormatPtrFunc)() = &DerivedB::SaveFormat;
derivedA->*SaveFormatPtrFunc();
// but that doesn't compile ;-(
}

Does anybody know what's the best way of doing this ?

The ONLY way of doing this is to implement your file-saving separately
from the data altogether. See "Visitor" pattern.

Neither 'DerivedA' nor 'DerivedB' should actually be derived from Base
if all you need to do is to save 'Base' in some specific format. You
need two classes 'SaverA' and 'SaverB', possibly derived from 'Saver',
and call those (possibly polymorphically) from 'Base' itself. The
pointer to the 'Saver' should be set by the derived class object:

class Saver
{
public:
virtual void save(class Base*);
};

class Base
{
...
virtual void saveSelf() { if (saver) saver->save(this); }
protected:
Saver* saver;
};

class SaverA : public Saver
{
void save(Base*) { ... } // format A
};

class SaverB : public Saver
{
void save(Base*) { ... } // format B
};

class DerivedA : public Base
{
public:
DerivedA() { saver = new SaverA; }
};

class DerivedB : public Base
{
public:
DerivedB() { saver = new SaverB; }
};

class CrossSaver : public Base
{
CrossSaver() { saver = 0; } // no saver
void saveSelf() {
SaverA sa;
saver = &sa; Base::saveSelf();
SaverB sb;
saver = &sb; Base::saveSelf();
}
};

V
 
B

Bo Persson

mast4as said:
Hi everyone

I have a problem that many people probably came across before but I
didn't find an answer on the net yet ...

I have a base class Bass and 2 derived class DerivedA & DerivedB. I
can create an new instance of the DeriveA class but then need to
call a method of the DerivedB class to save the data of the base
class into a specific file format. So I thought of using function
pointer but can seem to figure it out:

class Base
{
public:
float *data;
Base() { data = new float[10]; }
~Base() { delete [] data; }
virtual void SaveFormat() = 0;
}

class DerivedA : public Base
{
public:
void SaveFormat() { // save data in file format A }
}

class DerivedB : public Base
{
public:
void SaveFormat() { // save data in file format B }
}

int main()
{
Base *derivedA = new Derived A;
// now I want to save the data hold in derivedA but by using
SaveFormat from DerivedB ???
????
// this is where I am lost... I tried
void (Base::*SaveFormatPtrFunc)() = &DerivedB::SaveFormat;
derivedA->*SaveFormatPtrFunc();
// but that doesn't compile ;-(
}

Does anybody know what's the best way of doing this ?

Not this way for sure. :)

If you want different ways to save the data, without the format being
tied to the actual type of the objects, why make them members in the
first place?

What about a couple of free functions?

void SaveFormatA(const Base&);
void SaveFormatB(const Base&);


If you still need the virtual functions for a default save format,
these can call the free function of their choice, like

void DerivedA::SaveFormat()
{ SaveFormatA(*this); }


Bo Persson
 
M

mast4as

mast4as said:
Hi everyone
I have a problem that many people probably came across before but I
didn't find an answer on the net yet ...
I have a base class Bass and 2 derived class DerivedA & DerivedB. I
can create an new instance of the DeriveA class but then need to
call a method of the DerivedB class to save the data of the base
class into a specific file format. So I thought of using function
pointer but can seem to figure it out:
class Base
{
public:
float *data;
Base() { data = new float[10]; }
~Base() { delete [] data; }
virtual void SaveFormat() = 0;
}
class DerivedA : public Base
{
public:
void SaveFormat() { // save data in file format A }
}
class DerivedB : public Base
{
public:
void SaveFormat() { // save data in file format B }
}
int main()
{
Base *derivedA = new Derived A;
// now I want to save the data hold in derivedA but by using
SaveFormat from DerivedB ???
????
// this is where I am lost... I tried
void (Base::*SaveFormatPtrFunc)() = &DerivedB::SaveFormat;
derivedA->*SaveFormatPtrFunc();
// but that doesn't compile ;-(
}
Does anybody know what's the best way of doing this ?

Not this way for sure. :)

If you want different ways to save the data, without the format being
tied to the actual type of the objects, why make them members in the
first place?

What about a couple of free functions?

void SaveFormatA(const Base&);
void SaveFormatB(const Base&);

If you still need the virtual functions for a default save format,
these can call the free function of their choice, like

void DerivedA::SaveFormat()
{ SaveFormatA(*this); }

Bo Persson

Thanks everyone for your answer. Well I agree the design was ugly
which why I hoped you could give me some better ways of doing this ;-)
I made the Save method part of the class because each class uses
different ways of encoding the data and therefore I have also methods
in each of these classes that allow me to compress/decompress the data
according to the file format description.

The code from Victor obviously works fine but the DerivedA/B classes
don't only have methods to save the data, but of course also to read
the files and store the data into m_data from the base class (or
something of that kind). I also wanted to avoid having to create Temp
instances (such as in the example given by Victor where you need to
create sa sb).

Anyway I will digest some of the things you said and will go with a
simpler solution.

Thanks for your help -c
 
K

Krice

The code from Victor obviously works fine but the DerivedA/B classes
don't only have methods to save the data, but of course also to read
the files and store the data into m_data from the base class (or
something of that kind). I also wanted to avoid having to create Temp
instances (such as in the example given by Victor where you need to
create sa sb).

Create (non-virtual) save/load routines for each class,
responsible for storing and restoring the class data with
a handle to the stream or common save routine or whatever.
It's really that simple, when you think that class data is
just data. It can be loaded and saved, and that's everything
the data needs to know.
 
J

Juha Nieminen

mast4as said:
I have a base class Bass and 2 derived class DerivedA & DerivedB. I
can create an new instance of the DeriveA class but then need to call
a method of the DerivedB class to save the data of the base class into
a specific file format. So I thought of using function pointer but can
seem to figure it out:

You are trying to circumvent a design error by trying to use some
unsafe compiler trickery (which might or might not work) in order to get
around the problem. Hence, rather obviously, the *correct* solution to
this problem is not figuring out the syntax to achieve that trickery,
but to re-design your program properly.

An object of type DerivedA is *not* an object of type DerivedB, and
thus passing an object of type DerivedA to a member function of DerivedB
is extremely dubious at best, a maintenance nightmare at worst (and this
assuming it even works without trashing your object). Naturally the
standard makes no promises if you circumvent type safety like this.

So I recommend you rethink your class design in such way that doing
what you want can be done cleanly and logically, and without having to
resort to ugly hacks which circumvent the language type system.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top