c++ Derived class 改变一个基数 class 指针
c++ Derived class changing a base class pointer
我有一个 user class
,它有一个指向 data class
的成员指针。但我想实现一个扩展 user
的 derivedUser
,但它还需要存储在 derivedData class
中的额外数据,它看起来像这样:
class Data {
/*...the stored data...*/
}
class DerivedData : public Data {
/*...the additional data...*/
}
class User {
public:
/*...some methods that use dp...*/
protected:
Data* dp;
}
class DerivedUser : public User {
public:
/*...even more methods that use ddp...*/
protected:
DerivedData* ddp;
}
但问题是:按照我设置的方式,DerivedUser class
会存储两个指向同一对象的不同类型的指针,这不是最优的。 DerivedUser
应该只存储一个指针,它应该知道它是 DerivedData
类型,如果给它错误的数据类型就会失败。问题是:我该如何实施?
我试过:
class DerivedUser : public User {
public:
/*...even more methods that use ddp...*/
protected:
DerivedData* ddp = dynamic_cast<DerivedData*>(dp);
}
在 DerivedUser class
中添加方法 DerivedData* GetData()
DerivedData* GetData()
{
return static_cast<DerivedData>(dp);
}
如果 DerivedUser 的构造函数如下所示,您可以确保 dp
是 DerivedData*
:
DerivedUser(DerivedData* d):User(d){}
完整代码如下:
class Data {
/*...the stored data...*/
}
class DerivedData : public Data {
/*...the additional data...*/
}
class User {
public:
User(Data* d):dp(d){}
/*...some methods that use dp...*/
protected:
Data* dp;
}
class DerivedUser : public User {
public:
DerivedUser(DerivedData* d):User(d){}
/*...even more methods that use ddp...*/
protected:
DerivedData* GetData(void)
{
return static_cast<DerivedData*>(dp);
};
}
我看到您希望 DerivedUser 在其构造函数中包含 DerivedData。
由于多态性,父 class 可以引用它的子 class。所以这是合法的:
Data* dp = new DerivedData();
这是您正在寻找的解决方案:
class User {
public:
/*...some methods that use dp...*/
User(Data* dp){
this->dp = dp;
}
protected:
Data* dp;
};
class DerivedUser : public User {
public:
/*...even more methods that use ddp...*/
DerivedUser(DerivedData *dp) : User(dp) {
}
};
现在,DerivedUser 指向您的 DerivedData class
此处:
int main(){
DerivedData* dp = new DerivedData();
DerivedUser* user = new DerivedUser(dp);
return 0;
}
我有一个 user class
,它有一个指向 data class
的成员指针。但我想实现一个扩展 user
的 derivedUser
,但它还需要存储在 derivedData class
中的额外数据,它看起来像这样:
class Data {
/*...the stored data...*/
}
class DerivedData : public Data {
/*...the additional data...*/
}
class User {
public:
/*...some methods that use dp...*/
protected:
Data* dp;
}
class DerivedUser : public User {
public:
/*...even more methods that use ddp...*/
protected:
DerivedData* ddp;
}
但问题是:按照我设置的方式,DerivedUser class
会存储两个指向同一对象的不同类型的指针,这不是最优的。 DerivedUser
应该只存储一个指针,它应该知道它是 DerivedData
类型,如果给它错误的数据类型就会失败。问题是:我该如何实施?
我试过:
class DerivedUser : public User {
public:
/*...even more methods that use ddp...*/
protected:
DerivedData* ddp = dynamic_cast<DerivedData*>(dp);
}
在 DerivedUser class
中添加方法DerivedData* GetData()
DerivedData* GetData()
{
return static_cast<DerivedData>(dp);
}
如果 DerivedUser 的构造函数如下所示,您可以确保 dp
是 DerivedData*
:
DerivedUser(DerivedData* d):User(d){}
完整代码如下:
class Data {
/*...the stored data...*/
}
class DerivedData : public Data {
/*...the additional data...*/
}
class User {
public:
User(Data* d):dp(d){}
/*...some methods that use dp...*/
protected:
Data* dp;
}
class DerivedUser : public User {
public:
DerivedUser(DerivedData* d):User(d){}
/*...even more methods that use ddp...*/
protected:
DerivedData* GetData(void)
{
return static_cast<DerivedData*>(dp);
};
}
我看到您希望 DerivedUser 在其构造函数中包含 DerivedData。
由于多态性,父 class 可以引用它的子 class。所以这是合法的:
Data* dp = new DerivedData();
这是您正在寻找的解决方案:
class User {
public:
/*...some methods that use dp...*/
User(Data* dp){
this->dp = dp;
}
protected:
Data* dp;
};
class DerivedUser : public User {
public:
/*...even more methods that use ddp...*/
DerivedUser(DerivedData *dp) : User(dp) {
}
};
现在,DerivedUser 指向您的 DerivedData class
此处:
int main(){
DerivedData* dp = new DerivedData();
DerivedUser* user = new DerivedUser(dp);
return 0;
}