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

C++ polymorphism question

Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){};
void oskar(void)
{emil();};};

A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

Regards,
Jonas

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo

Sep 19 '07 #1
8 1343
Jonas Huckestein wrote:
Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){};
the ';' not needed
void oskar(void)
{emil();};};
the ';' after the function is not needed ... don't put it at the end of
functions.
>
A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.
This is allowed. Not only is it allowed, it is the intention for
virtual functions to have access to the class it is defined in.

Don't call it from "A"'s constructor though, you'll get surprises.

Sep 19 '07 #2
>A* peter = new B;
>B->oskar();
Is there a typo here? This does not demonstrate what you're asking, I
guess.
Sep 19 '07 #3
On 2007-09-19 13:22, Jonas Huckestein wrote:
Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){};
void oskar(void)
{emil();};};

A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.
As D. Susman pointed out, this does not look like what you asked for,
perhaps you meant something like this:

class A {
virtual void foo();
};

class B : public A {
void foo() { bar(); }; // Call B::bar() in B::foo()
void bar();
};

int main() {
A* a = new B;
a->foo(); // This will call B::bar();
}

--
Erik Wikström
Sep 19 '07 #4
Gianni Mariani wrote:
Jonas Huckestein wrote:
>Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){};
the ';' not needed
> void oskar(void)
{emil();};};
the ';' after the function is not needed ... don't put it at the end of
functions.
not "not needed", but even wrong, but I'm sure the standard allow extra
';' in class definition or not; AFAIK, some library like /CppUnit/ even
borrows some technique to avoid "extra ';' error" for some compilers,
what compile(s)? I don't know.
>
>>
A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

This is allowed. Not only is it allowed, it is the intention for
the code is even illformed

if /A/ makes /oskar/ public and /B/ uses public inheritance
then we can write
peter->oskar();

I think this is the OP's intent
virtual functions to have access to the class it is defined in.

Don't call it from "A"'s constructor though, you'll get surprises.

--
Thanks
Barry
Sep 19 '07 #5
Erik Wikström wrote:
On 2007-09-19 13:22, Jonas Huckestein wrote:
>Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);
};

class B : A {
void emil(){};
void oskar(void)
{emil();};};

A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

As D. Susman pointed out, this does not look like what you asked for,
perhaps you meant something like this:

class A {
public:
virtual void foo();
};

class B : public A {
void foo() { bar(); }; // Call B::bar() in B::foo()
void bar();
};

int main() {
A* a = new B;
a->foo(); // This will call B::bar();
}

--
Thanks
Barry
Sep 19 '07 #6
In article
<1190202346.278828.54350@o80g
2000hse.googlegroups.com"D.
Susman"
<de**********@gmail.com>
wrote:
> A* peter = new B;
B->oskar();
Is there a typo here? This
does not demonstrate what
you're asking, Iguess.

Oh yeah, thanks :) It should
have been peter->oskar() :)

Regards,
Jonas

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo

Sep 19 '07 #7
On Sep 20, 12:02 am, Erik Wikström <Erik-wikst...@telia.comwrote:
perhaps you meant something like this:

class A {
virtual void foo();
};

class B : public A {
void foo() { bar(); }; // Call B::bar() in B::foo()
void bar();

};

int main() {
A* a = new B;
a->foo(); // This will call B::bar();
}
You need to make A::foo public.

Sep 21 '07 #8
On 19 Sep, 12:22, Jonas Huckestein <swap mac....@jonas.huckestein>
wrote:
Hi there,

I was wondering, whether I
can access members of a
derived class from within
it, if I accessed it from a
virtual function on the base
classpointer ^^ Example:

class A {
virtual void oskar(void);

};

class B : A {
void emil(){};
void oskar(void)
{emil();};};

A* peter = new B;
B->oskar();

Is this allowed? I know it
is not possible to call emil
directly, but thiswould be a
fine workaround.

Regards,
Jonas

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it athttp://www.malcom-mac.com/nemo
This would be allowed;

class A
{
public:
virtual void oskar(); // style issue: do not put void parameter in
but technically allowed

// A should also have a virtual destructor the way you use it thus:
virtual ~A() {}
};

class B : public A // note it must inherit public to use the
inheritance outside of class B itself
{
void emil(){};

void oskar() // it is allowed to be private here and will still
work
{
emil();
}
};

int main() // or any other function
{
A * peter = new B;
peter->oskar(); // allowed as long as oskar is public in A or this
function is a friend

delete peter; // and hope oskar() didn't throw, so preferably use
auto_ptr or scoped_ptr
// but we are illustrating access here
}

Another popular "style" issue is to make the virtual method protected
in A and have a public non-virtual method call it. (It is better to
make it protected rather than private, not because the derived class
can't override the private virtual, it can, and not because it "looks
confusing" (like the FAQ says) but because it cannot implement in
terms of its base class implementation).

Sep 21 '07 #9

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

Similar topics

3
by: Mayer Goldberg | last post by:
Can someone please explain the motivation behind the following restriction in the language: I define an interface and two classes implementing it: public interface InterA {} public class B...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
3
by: John Salerno | last post by:
Along with events and delegates, polymorphism has been something I sort of struggle with every now and then. First, let me quote the book I'm reading: "Polymorphism is most useful when you have...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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,...

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.