简体   繁体   中英

Assigning and Accesing derived class object to base class “pointer to pointer” object in C++

I am very much new to c++. I have a situation like: I have a base class which holds two virtual functions(NOT PURE). I have a derived class for this class where I implemented those virtual functions. Now in my main() function I created a pointer to pointer object to the base class. Now using this object how can I access the derived class object and functions.

I only want the pointer to pointer object of base class should be used to access derived class object.

BASE CLASS:

class another
{
    public:
    virtual void setName(){};
    virtual string getName(){};
};

Derived Class

class use: public another
{
    public:
        string str;
    void setName()
        {
            str = "USE CLASS";
        }
        string getName()
        {
            return str;
        }
};

MY main() function:

int main()
{
    another **an;
    *an = new use();
    an->setName();  //getting error
    cout<<an->getName()<<endl; //getting error
    return 0;

}
*an = new use();

Pointer an is not initialized, it cannot be dereferenced. Using doubled pointer (pointer to pointer) got no practical sense here. All tit does is to add another level of reference to code in this case. Expression that dereferences such pointer, results in value of pointer to class 'another', stored ... where? You had never created that storage, so such operation is an UB.

Legal variants of code:

int main()
{
    another **an = new another*(); // creating storage for pointer
    *an = new use();
    (*an)->setName();  
    cout<<(*an)->getName()<<endl; 
    delete *an;  // don't rely on OS to do so.
    delete an;
    return 0;
}


int main()
{
    another **an = new another*(new use()); // doing this in one line
    // storage of *an would be value-initialized by value returned
    // from 'new use()' instead of default initialization
    (*an)->setName();  
    cout<<(*an)->getName()<<endl; 
    delete *an;  // don't rely on OS to do so.
    delete an;
    return 0;
}

int main()
{
    another *an = new use(); 
    // We don't need another pointer~ use an reference where required?

    an->setName();  
    cout<<an->getName()<<endl; 
    delete an;  // don't rely on OS to do so.
    return 0;
}

PS. This declaration of class another technically is ill-formed, I may assume that you had skipped body of getName. It should cause compile time error about function's missing return value. If another is unusable class itself, you may declare methods as pure

class another
{
    public:
    virtual void setName() = 0;
    virtual string getName() = 0;
};

An instance of such class or of a derived class that DOES NOT override those methods cannot be created, but it provides that "boilerplate" mechanics you're studying.

an is a pointer to pointer to another . *an returns a pointer to another . So you want (*an)->setName(); like so:

int main()
{
    another **an;
    *an = new use();
    (*an)->setName();  //getting error
    cout<<(*an)->getName()<<endl; //getting error
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM