473,770 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"virtual" variables?

Hi,
i have a StuffProducer(S Prod) 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=NUL L) ERROR" on the members... but there
must be a more 1337 solution ;)

anyway thanks for any hint!!

May 12 '06 #1
4 1774

<mw****@freenet .de> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
Hi,
i have a StuffProducer(S Prod) 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=NUL L) 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_pC ontainer) {}

// 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 producingFuncti on(){
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.goo glegroups.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(&containe r){}

//a Function producing values and storing in the container
void producingFuncti on(){
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(v oid) 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
1910
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
1903
by: Duck Dodgers | last post by:
Here is my situation class base { }; class child1 { int data; }; class child2 {
6
9772
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: Condition() {} virtual ~Condition() {} virtual bool check() const;
175
8907
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 changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
4
8507
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? -- programmer, author http://www.midnightbeach.com and father http://www.midnightbeach.com/hs
7
2479
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 has nothing to do with making multiple arguments in a declaration like:
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.