简体   繁体   中英

method of a pointer pointing to an object is inaccesible

my class looks like this

class A{
private:
    int id;
public:
    A();
    int getId();
}

class B{
private:

public:
  B();
}

Implementation

//constructor
    B::B() : A(){
    }

B extends A class.

Okay in my main cpp I have a function like this

bool checkID(B *obj){
    if(obj->getId() > 1){ return true; } else { return false; }

}

However, the obj-> getId() , getId() says its inaccesible.

Why is it?

By default C++ uses private inheritance. Use B : public A to publicly inherit from A. Also you need to define the inheritance in the declaration.

class B : public A {
 public:
  B();
}

B必须派生自A,才能使B的对象可以访问A的成员。

class B : public A { ... };

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