C++ Access derived class member from base class pointer

accessing derived class members from base class c
base class pointer to derived class object c++
base class reference derived class object
how to access base class members using derived class object
can base class access members of derived class give reasons
c++ abstract base class
array of base class pointers c++
virtual function in c++

If I allocate an object of a class Derived (with a base class of Base), and store a pointer to that object in a variable that points to the base class, how can I access the members of the Derived class?

Here's an example:

class Base
{
    public:
    int base_int;
};

class Derived : public Base
{
    public:
    int derived_int;
};

Base* basepointer = new Derived();
basepointer-> //Access derived_int here, is it possible? If so, then how?

No, you cannot access derived_int because derived_int is part of Derived, while basepointer is a pointer to Base.

You can do it the other way round though:

Derived* derivedpointer = new Derived;
derivedpointer->base_int; // You can access this just fine

Derived classes inherit the members of the base class, not the other way around.

However, if your basepointer was pointing to an instance of Derived then you could access it through a cast:

Base* basepointer = new Derived;
static_cast<Derived*>(basepointer)->derived_int; // Can now access, because we have a derived pointer

Note that you'll need to change your inheritance to public first:

class Derived : public Base

[C++] Accessing derived class members from base class pointer , A base class 'object' and two derived classes, "ship" and "laser". "ship" has a method called fire(). I've also got an object manager that stores all the existing objects  The code you posted will not compile because the compiler has no Idea what derived class is while compiling the code for class Base. the this pointer is a pointer that is implicitly passed to every non static member function of a class its how that function can access member attributes of the it's instance.


You're dancing on the minefield here. The base class can never know that it's actually an instance of the derived. The safest way to do that would be to introduce a virtual function in the base:

class Base 
{ 
protected:
 virtual int &GetInt()
 {
  //Die horribly
 }

public: 
 int base_int; 
}; 

class Derived : Base 
{ 
  int &GetInt()
  {
    return derived_int;
  }
public: 
int derived_int 
}; 

basepointer->GetInt() = 0;

If basepointer points as something other that a Derived, your program will die horribly, which is the intended result.

Alternatively, you can use dynamic_cast<Derived>(basepointer). But you need at least one virtual function in the Base for that, and be prepared to encounter a zero.

The static_cast<>, like some suggest, is a sure way to shoot yourself in the foot. Don't contribute to the vast cache of "unsafety of the C language family" horror stories.

Accessing derived class members using base class pointer., Friends, Can a base class reference be used to access derived class-specific members? Note that the members are not part of the base class,  Pointer to Derived Class Object In C++ you can declare a pointer that contains the address of the object of type class. Suppose we have created a class named base as shown below: class Base { public: int x; void display () { cout<<”X=”<<x<<endl; } };


you can use CRTP

you basically use the derived class in the template for the base class

Accessing derived class functions through pointer of base , The base class has a pure virtual function called getInfo() that is meant to get the info from the objects of the derived classes. So in my main I have. It turns out that because rBase and pBase are a Base reference and pointer, they can only see members of Base (or any classes that Base inherited). So even though Derived::getName () shadows (hides) Base::getName () for Derived objects, the Base pointer/reference can not see Derived::getName ().


It is possible by letting the base class know the type of derived class. This can be done by making the base class a template of derived type. This C++ idiom is called curiously recurring template pattern.

Knowing the derived class, the base class pointer can be static-casted to a pointer to derived type.

template<typename DerivedT>
class Base
{
public:
    int accessDerivedField()
    {
        auto derived = static_cast<DerivedT*>(this);
        return derived->field;
    }
};


class Derived : public Base<Derived>
{
public:
    int field;
};

int main()
{
    auto obj = new Derived;
    obj->accessDerivedField();
}

12.1, Why would I set a pointer or reference to the base class of a derived object say you had 3 cats and 3 dogs that you wanted to keep in an array for easy access. base class (much like the Animal's name is accessible via member m_name). each class and updated with C++11 concepts like a for-each loop for exercise):. If the member variables in the base class are listed as protected, then who can access or modify those variables? a. friends of the base class b. friends of the derived class c. members of the base class d. members of the derived class e. A and B f. C and D g. All of the above


//if you know what derived class you are going to use

Derived* derivedpointer = dynamic_cast < Derived * > basepointer;

//then you can access derived class using derivedpointer

[PDF] Derived Classes and Inheritance Derived Classes, Class that inherits data members and member functions from a previously protected: Derived classes and friends can access protected members 108// Casting base-class pointers to derived-class pointers Area of c (via circlePtr): 22.90. It is possible by letting the base class know the type of derived class. This can be done by making the base class a template of derived type. This C++ idiom is called curiously recurring template pattern. Knowing the derived class, the base class pointer can be static-casted to a pointer to derived type.


Access control of base class members (C++ only), Access control of base class members (C++ only) You can derive classes using any of the three access specifiers: In a public base class, public and protected members of the base class remain public and Members and friends of a class can implicitly convert a pointer to an object of that class to a pointer to either:. A class in C++ has public, private and protected sections which contain the corresponding class members. Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.


Polymorphism - C++ Tutorials, int A::b(int c) { }, Classes One of the key features of class inheritance is that a pointer to a derived class is Dereferencing ppoly1 and ppoly2 (with *ppoly1 and *ppoly2 ) is valid and allows us to access the members of their pointed objects. Pointer of base class points to it n1 = 44 Pointer of base class points to derive class n1=66 Here the show() method is the overridden method, bptr execute show() method of ‘base’ class twice and display its content. Even though bptr first points to ‘base’ and second time points to ‘derive’ ,both the time bptr->show() executes the


Regarding C++, why can't pointers to base classes not access , Regarding C++, why can't pointers to base classes not access members of a derived class? Ad by F5. Base class can access members functions of derived class if they. were declared as 'virtual' in base class and have same prototype. For this type of access, a pointer to base class is required.