473,383 Members | 1,837 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,383 software developers and data experts.

Virtual Variables?


I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions
within msg_lin, How would I do this? I thought putting 'virtual int
count;' in msg_lin might work, but I guess it doesn't...

#include <cstdio>

class msg_lin {
/*Doesn't work right, I want count to be the dsply_stuff::count,
not msg_lin::count*/
virtual int count;
public:
bool msg(void);
};
#endif

bool msg_lin::msg(void) {
/*this is supposed to display dsply_stuff::count, not msg_lin's
*/
printf("count: %d\n", count);
}
class dsply_stuff : public msg_lin {
int count;
public:
dsply_stuff(int var);
};
#endif

dsply_stuff::dsply_stuff(int var) {
count = var;
}
int main(void) {
dsply_stuff display(7);
dsply_stuff *p_dsply;
p_dsply = &display;

p_dsply->msg(); //should display count: 7

getchar();
}

May 4 '07 #1
8 11244
BWIGLEY wrote:
I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions
within msg_lin, How would I do this? I thought putting 'virtual int
count;' in msg_lin might work, but I guess it doesn't...
Use a virtual function:

#include <iostream>

class msg_lin {
virtual int count() const = 0;
public:
void msg();
};

void msg_lin::msg(void) {
std::cout << "count: " << count() << std::endl;
}

class dsply_stuff : public msg_lin {
int value;
int count() const { return value; }

public:
dsply_stuff(int var);
};

dsply_stuff::dsply_stuff(int var) {
value = var;
}
>

int main(void) {
dsply_stuff display(7);
dsply_stuff *p_dsply;
p_dsply = &display;

p_dsply->msg(); //should display count: 7

getchar();
}

--
Ian Collins.
May 4 '07 #2
On Fri, 04 May 2007 20:51:16 +1200, BWIGLEY wrote:
I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions within
msg_lin, How would I do this? I thought putting 'virtual int count;' in
msg_lin might work, but I guess it doesn't...

#include <cstdio>

class msg_lin {
/*Doesn't work right, I want count to be the dsply_stuff::count,
not msg_lin::count*/
virtual int count;
public:
bool msg(void);
};
#endif

bool msg_lin::msg(void) {
/*this is supposed to display dsply_stuff::count, not msg_lin's */
printf("count: %d\n", count);
}
class dsply_stuff : public msg_lin {
int count;
public:
dsply_stuff(int var);
};
#endif

dsply_stuff::dsply_stuff(int var) {
count = var;
}
int main(void) {
dsply_stuff display(7);
dsply_stuff *p_dsply;
p_dsply = &display;

p_dsply->msg(); //should display count: 7

getchar();
}
Something like this?

#include <cstdio>

class msg_lin
{
public:
virtual int get_count() const =0; // pure virtual - derived class must implement

void msg() const
{
printf("count: %d\n", get_count()); // uses derived class implementation of get_count()
}
};

class dsply_stuff : public msg_lin
{
int count;

public:
dsply_stuff(int var) : count(var) // prefer initialisation to assignment
{
}

virtual int get_count() const // implements base class pure virtual
{
return count;
}
};
int main()
{
dsply_stuff display(7);

msg_lin* p = &display;

p->msg();
}

--
Lionel B
May 4 '07 #3
On May 4, 1:51 pm, "BWIGLEY" <bwig...@ihug.co.nzwrote:
I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions
within msg_lin, How would I do this? I thought putting 'virtual int
count;' in msg_lin might work, but I guess it doesn't...

#include <cstdio>

class msg_lin {
/*Doesn't work right, I want count to be the dsply_stuff::count,
not msg_lin::count*/
virtual int count;
It's not possible to declare a data member as virtual data member.
Compiler should've thrown compilation err, I guess.

-
Sukumar R

May 4 '07 #4
"Ian Collins" <ia******@hotmail.comwrote in message
news:5a*************@mid.individual.net...
BWIGLEY wrote:
I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions
within msg_lin, How would I do this? I thought putting 'virtual
int
count;' in msg_lin might work, but I guess it doesn't...
Use a virtual function:
I guess I may have over simplified a little... the variable will
actually be a pointer to a display function like this(sort-of
incomplete code):
#include <iostream>

class display_object {
public:
display();
};

class msg_lin {
public:
void msg() {
p_dsply_obj->display("This text would be displayed");
}
};

class dsply_stuff : public msg_lin {
display object *p_dsply_obj;
int count() const { return value; }

public:
dsply_stuff(int var);
};

dsply_stuff::dsply_stuff(int var) {
p_dsply_obj = var;
}

int main(void) {
display_object dsply_obj
dsply_stuff display(&dsply_obj);
dsply_stuff *p_dsply;
p_dsply = &display;

p_dsply->msg(); //should display the message

getchar();
}

p_dsply_obj wouldn't need to be changed after intilization. Is there
a better way to make msg_lin know about p_daply_obj?
May 4 '07 #5
BWIGLEY wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5a*************@mid.individual.net...
>BWIGLEY wrote:
>>I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions
within msg_lin, How would I do this? I thought putting 'virtual
int
>>count;' in msg_lin might work, but I guess it doesn't...
Use a virtual function:

I guess I may have over simplified a little... the variable will
actually be a pointer to a display function like this(sort-of
incomplete code):
Well use a virtual function to return the pointer!

--
Ian Collins.
May 4 '07 #6
"Ian Collins" <ia******@hotmail.comwrote in message
news:5a*************@mid.individual.net...
BWIGLEY wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5a*************@mid.individual.net...
BWIGLEY wrote:
I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from
functions
>within msg_lin, How would I do this? I thought putting 'virtual
int
>count;' in msg_lin might work, but I guess it doesn't...

Use a virtual function:
I guess I may have over simplified a little... the variable will
actually be a pointer to a display function like this(sort-of
incomplete code):
Well use a virtual function to return the pointer!
Is this the only way to do it? it seems a little icky. If it is the
only way would it be better to just set a msg_lin's *p_dsply_obj to
&display on intilization and work off p_dsply_obj?
May 4 '07 #7
"BWIGLEY" <bw*****@ihug.co.nzwrote in message
news:f1**********@lust.ihug.co.nz...
>
I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions
within msg_lin, How would I do this? I thought putting 'virtual int
count;' in msg_lin might work, but I guess it doesn't...

#include <cstdio>

class msg_lin {
/*Doesn't work right, I want count to be the dsply_stuff::count,
not msg_lin::count*/
virtual int count;
public:
bool msg(void);
};
#endif

bool msg_lin::msg(void) {
/*this is supposed to display dsply_stuff::count, not msg_lin's
*/
printf("count: %d\n", count);
}
class dsply_stuff : public msg_lin {
int count;
public:
dsply_stuff(int var);
};
#endif

dsply_stuff::dsply_stuff(int var) {
count = var;
}
int main(void) {
dsply_stuff display(7);
dsply_stuff *p_dsply;
p_dsply = &display;

p_dsply->msg(); //should display count: 7

getchar();
}
Okay, you have a base class that needs a variable defined in the derived.
So, why not just define it in your base? From your description later, it
seems it's actually a pointer. That's fine, just have it initialized in the
base's constructor.
#include <cstdio>

class msg_lin {
/*Doesn't work right, I want count to be the dsply_stuff::count,
not msg_lin::count*/
// virtual int count;

protected:
int* count; // or whatever pointer it is
msg_lin( int* countP): count( countP ) {}; // count gets initialized in
constructor
// since it's a protected constructor, only a derived class can
instantize
public:
bool msg(void);
};
#endif
// bool msg_lin::msg(void) {
bool msg_lin::msg() {
// void is not needed, nor desired, in C++ as a parm.
/*this is supposed to display dsply_stuff::count, not msg_lin's
*/
// printf("count: %d\n", count);

printf("count %d\n", *count ); // Just cause it's a ponter now
}

class dsply_stuff : public msg_lin {
// int count;
// don't need anymore, it's in base now
public:
dsply_stuff(int* var);
};
#endif
//dsply_stuff::dsply_stuff(int var) {
dsply_stuf::dsply_stuff( int* var ): msg_lin( var ) {
// and that's where the magic comes in. The int* in msg_line gets
initialized here in
// the derived classe's initialization list.
count = var;
}
int main(void) {
dsply_stuff display(7);
dsply_stuff *p_dsply;
p_dsply = &display;

p_dsply->msg(); //should display count: 7

getchar();
}
Since that may be a little confusing because of all the code and comments,
I'll try to explain it a little better, if I can. If a base class needs a
variable defined/declared by a derived class, it generally means it just has
to be constructed in the base class's constructor, and then it is up to the
derived class to initalize the base class. It's generally better to use a
reference instead of a pointer, but same thing applies.

Here's an example:

class Base
{
public:
Base( sometype* somevar ): SomeVar( somevar ) {};
private:
sometype* SomeVar;
};

Now, it is impossible to instantize that Base class without giving it a
paramter, in this case a sometype reference. This would NOT compile: - for
this I did a
typedef int sometype;

class Derived: public Base
{
};

int main(void)
{
Derived d;
}

My compiler gives me:

warning C4510: 'Derived' : default constructor could not be generated
see declaration of 'Derived'
warning C4610: class 'Derived' can never be instantiated - user defined
constructor required
error C2512: 'Derived' : no appropriate default constructor available

Which is good, which is what we want. SomeVar HAS to be intialized in the
derived constructor, just like you want.

Now, changed it to this:

class Derived: public Base
{
public:
Derived( int* p ): Base(p) {};
};

int main(void)
{
int i;
Derived d(&i);

std::string wait;
std::getline( std::cin, wait );
}

and it compiles happily. If you want Base to only be able to be instantized
by a derived class, make the constructor protected.

class Base
{
protected:
Base( sometype* somevar ): SomeVar( somevar ) {};
private:
sometype* SomeVar;
};

and, again, it compiles and runs happily. But I can't do this:

int main()
{
int i;
Base b(&i);
}

My compiler telling me

error C2248: 'Base::Base' : cannot access protected member declared in class
'Base'
see declaration of 'Base::Base'
see declaration of 'Base'

This is all through the magic of "initalization lists". It won't work this
way without them.
May 5 '07 #8

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:lW***************@newsfe02.lga...
"BWIGLEY" <bw*****@ihug.co.nzwrote in message
news:f1**********@lust.ihug.co.nz...

I have an object (dsply_stuff), which inherits msg_lin and has a
variable count. I want to be able to access count from functions
within msg_lin, How would I do this? I thought putting 'virtual
int
count;' in msg_lin might work, but I guess it doesn't...
<snip nice explanation>

Thank's for that, it really clears things up...
May 5 '07 #9

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

Similar topics

3
by: Dave Ainslie | last post by:
How do I send session variables between two web directories? I have two sites on my web server I wish to send cookies between. Thanks in advance Dave
7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
1
by: Juan Daniel Caicedo | last post by:
I create an user session with ASP in a Virtual Folder. When the ASP try to delete a subfolder in the same virtual folder app, FSO delete the folder but they clean the session and clean all...
7
by: verbatime | last post by:
Please explain me how this works - or should work: Got my two classes - bcBasic (baseclass) and the derived cBasic. //--------------------------------------- class bcBasic { int number;...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
4
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
10
by: Nils Erik Dall | last post by:
Hi All I have a problem with my asp code on an iis 6.0 server on windows 2003 web. When I redirect between to asp pages on my web-site, where pageA is in a different virtual directory from pageB...
5
by: Terry On Windigo | last post by:
I think I have figured out my problem but I don't know how to solve it. We are going to start using a forums package and do not want our users to have to login to both our site, and then again to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.