473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

"virtual" variables?

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!!

May 12 '06 #1
4 1754

<mw****@freenet.de> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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
May 12 '06 #2
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);

}
};

May 12 '06 #3

<mw****@freenet.de> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
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
May 13 '06 #4
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
May 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: mshetty | last post by:
Hi, I get an error "Warning: b::a_method hides the virtual function a::a_method()." on compiling the following code.. #include <iostream.h> class a {
5
by: Duck Dodgers | last post by:
Here is my situation class base { }; class child1 { int data; }; class child2 {
6
by: kelvSYC | last post by:
This little bit of seeminly innocent code seems to give me these two errors, all on the line that declares check(). Is there some part of C++ that I'm missing out on? class Condition { public:...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
4
by: Jon Shemitz | last post by:
Syntax seems to say "modifiers is modifiers" and either is valid, but Google searches seem to show that "public virtual" is more common. Is there any sort of (semi) official convention? -- ...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.