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

Accessing derived class data members

How can i access a public data member of a derived class with a pointer to the base class.

class Base
{
public:
Base(){};
virtual ~Base(){};
virtual bool decode(AOC_String element_text) ;
}
class Derived: pubilc Base
{
pubilc:
int ele;
bool decode(AOC_String element_text)
{
}
}

Base *Ptr = (Base *)new(Derived);

Ptr->decode("123"); //works fine
Ptr->ele; //this throws an error... why is this wrong, what's the right way

Many Thanks
Jun 20 '07 #1
3 2331
Meetee
931 Expert Mod 512MB
How can i access a public data member of a derived class with a pointer to the base class.

class Base
{
public:
Base(){};
virtual ~Base(){};
virtual bool decode(AOC_String element_text) ;
}
class Derived: pubilc Base
{
pubilc:
int ele;
bool decode(AOC_String element_text)
{
}
}

Base *Ptr = (Base *)new(Derived);

Ptr->decode("123"); //works fine
Ptr->ele; //this throws an error... why is this wrong, what's the right way

Many Thanks
Pointer *Ptr is a base pointer, and decode is a virtual method. So base pointer can access this method and not ele.
Jun 20 '07 #2
Pointer *Ptr is a base pointer, and decode is a virtual method. So base pointer can access this method and not ele.
Is there any way by which i can access the Element of the derived class from *Ptr?
Jun 20 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Base *Ptr = (Base *)new(Derived);

Ptr->decode("123"); //works fine
Ptr->ele; //this throws an error... why is this wrong, what's the right way
Stop casting. The code should be:

Expand|Select|Wrap|Line Numbers
  1. Base *Ptr = new Derived;
  2.  
  3. Ptr->decode("123"); //works fine
  4. Ptr->ele; //this throws an error... why is this wrong, what's the right way
  5.  
Ptr is a Base* so it can access only Base members.

If you need to use Ptr to access ele, then you need to design your classes to support the Visitor design pattern.
Jun 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
6
by: harry | last post by:
Hi ppl I have a question about memory layout of a class. Consider the code below: class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" <<...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
0
by: Ignace 'a0a' Saenen | last post by:
Hi, I have actually found 2 ways to do this. One uses the XmlInclude attribute in the base class A, specifying the valid derived types to deserialize from the stream, and one uses one of the 8...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.