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

Pointers and Inheritance

Hi,

I am trying to get a pointer created in a derived class to be accesible and used in the base class. Is this actually possible and how would one call that pointer?

Thankyou for your help in advance :)

- Muffins
Nov 25 '08 #1
5 1875
boxfish
469 Expert 256MB
How is the pointer created in the base class? If it's just a member of the base class that's inherited by the derived class, you should be able to use it just like any other inherited member. You're just refering to any sort of dynamic array or variable, right?
Nov 25 '08 #2
Ganon11
3,652 Expert 2GB
If the pointer is made and used in the derived class, it cannot be viewed in the base class. Consider the following simple outline:

Expand|Select|Wrap|Line Numbers
  1. class Person {
  2.    // ... stuff here.
  3.    // for example,
  4.    public:
  5.       string name;
  6.       int age;
  7. };
  8.  
  9. class Student : public person {
  10.    // ... stuff here.
  11.    // for example
  12.    public:
  13.       double GPA;
  14. };
A Student object has a name, age, and GPA, but a Person object does not have a GPA, because a Person is NOT a Student. A Student IS-A Person, not vice versa.

Now, why do you need to use a derived class pointer in a base class? It would seem prudent to make this pointer in the base class so that both would have access to it.
Nov 25 '08 #3
Thank you both for your reply. It is actually a requirement of an assigment I am doing that the pointer has to be made in the sub class and then used in the base class, or at least reference is made to it in the base class. Hope I am making some sense.
Nov 25 '08 #4
JosAH
11,448 Expert 8TB
@muffins
You could make an object of the base class ask for that pointer through a small virtual method. The first virtual method in the base class can simply return NULL, the overridden function implementation(s) in the derived class(es) should return the actual value of such a pointer.

kind regards,

Jos
Nov 25 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
Use the Visitor design pattern for this.

You visit your base class with a pointer to a visitor. The derived class overrides the base class visit method and calls a visitor method using the derived class pointer.

Done every day.
Nov 25 '08 #6

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
3
by: Mattias Brändström | last post by:
Hello! Please consider the code below: class A { public: virtual void foo(int i) = 0; }; class B {
7
by: alex.mizrahi | last post by:
helo i'm going to use pointers to member function with virtual inheritance in following way: struct base { public: typedef void (base::*fun_t)(void); int i; void call(fun_t f) {
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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...

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.