"virtual" variables?

M

mwebel

Hi,
i have a StuffProducer(SProd) and a StuffContainer(SCont) basis class.

-SProd produces values, that are stored in SCont.
-Derived classes of both are compatible only on the same derivated
path.(SProdA with SContA, SProdB with SContB and so on ).


SProd should have always a intance of its respective Container as a
private variable.
My question is: how can i ensure that ppl who derive from it do
instantiate a Derived container within the producer?
pure virtual functions do make sure ppl will write the body for the
function...
i was thinking of placing a pointer of the base container class in the
base producer class (both abstract base classes (ABCs) ) and hope ppl
wont forget to set this pointer to the right derivation... well the
"right" derivation is not that important as to a derivation at all.
Some implemented member functions assume the pointer has been
asignated!

Example:
//******************
class SCont{};

class SProd{
protected:
SCont * pContainer;
void func(){//pContainer is used here!!};
};



//now comes the ideal implementation
class SContA:SCont{};

class SProdA:SProd{
private:
SContA contA;

pContainer = &contA; // <=This must happen!!!

}
//******************


is it ok to let it this way and tell everybody not to forget to assign
an object to the pointer or can i make sure the pointer will be set?

i could put a "if (pContainer=NULL) ERROR" on the members... but there
must be a more 1337 solution ;)

anyway thanks for any hint!!
 
C

Cy Edmunds

Hi,
i have a StuffProducer(SProd) and a StuffContainer(SCont) basis class.

-SProd produces values, that are stored in SCont.
-Derived classes of both are compatible only on the same derivated
path.(SProdA with SContA, SProdB with SContB and so on ).


SProd should have always a intance of its respective Container as a
private variable.
My question is: how can i ensure that ppl who derive from it do
instantiate a Derived container within the producer?
pure virtual functions do make sure ppl will write the body for the
function...
i was thinking of placing a pointer of the base container class in the
base producer class (both abstract base classes (ABCs) ) and hope ppl
wont forget to set this pointer to the right derivation... well the
"right" derivation is not that important as to a derivation at all.
Some implemented member functions assume the pointer has been
asignated!

Example:
//******************
class SCont{};

class SProd{
protected:
SCont * pContainer;

Don't use protected data anymore than you would use public data and for
pretty much the same reason.
void func(){//pContainer is used here!!};
};



//now comes the ideal implementation
class SContA:SCont{};

class SProdA:SProd{
private:
SContA contA;

pContainer = &contA; // <=This must happen!!!

This shouldn't happen! hehe Again, protected data members is bad form.
}
//******************


is it ok to let it this way and tell everybody not to forget to assign
an object to the pointer or can i make sure the pointer will be set?

i could put a "if (pContainer=NULL) ERROR" on the members... but there
must be a more 1337 solution ;)

anyway thanks for any hint!!

// 133t version
class SProd
{
private:
SCont * pContainer;

protected:
SCont *
psc() {return pContainer;}

SCont const * // support for const methods
psc() const {return pContainer;}

public:
// use a constructor to make sure pContainer is set
SProd(SCont *i_pContainer) : pContainer(i_pContainer) {}

// other stuff using psc()
};

In this version pContainer is guaranteed to be set and can't be changed by
the derived class once it is.

Cy
 
M

mwebel

hi,
first of all thanks for answering... now i must say i am not sure of
the way it works.
I forgot to say SCont has some member functions.

do you mean a derived class should look like this?:


//A derived class of the producer
class SProdA: SProd{

private:
//A derived Class of the container
SContA container;

public:
//constructor of derived class
SProdA(): SProd(container){}

//a Function producing values and storing in the container
void producingFunction(){
psc()->setValue(2);
}

//A func retrieving values from the container
void showingFunction(){
cout << psc()->getValue(2);

}


};
 
C

Cy Edmunds

hi,
first of all thanks for answering... now i must say i am not sure of
the way it works.
I forgot to say SCont has some member functions.

This is not a problem as your example below shows.
do you mean a derived class should look like this?:


//A derived class of the producer
class SProdA: SProd{

private:
//A derived Class of the container
SContA container;

public:
//constructor of derived class
SProdA(): SProd(container){}

should be SProdA(): SProd(&container){}
//a Function producing values and storing in the container
void producingFunction(){
psc()->setValue(2);
}

//A func retrieving values from the container
void showingFunction(){
cout << psc()->getValue(2);

}


};

I'm not sure I'm following all that but it seems OK
 
B

benben

I am not quite sure what exactly you want to achieve. I guess the
following is what you want:

For every class T derived from SProd there should exist a uniform way to
botain a pointer to type S, which must be derived from SCont but not
SCont per se.

If my assumption is correct then this can be achieved trivially by
making the constructor of SProd and SCont protected:

class SProd
{
protected:
virtual SCont* get_container(void) const = 0;
SProd();

// ...
};

class SCont
{
protected:
SCont();

// ...
};

Regards,
Ben
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top