多态性问题 C++

发布于 2024-11-07 04:12:58 字数 471 浏览 1 评论 0原文

我有这些类:

class A
{
};

class B : public A
{
public:
    B(int val);
private:
    int* m_int;
};

B::B(int val)
{
    m_int = &val;
}

我像这样调用代码,在 1 个函数内,我知道变量一旦超出范围就会被销毁,但我只需要在这个函数中使用它们:

{
...
int _int = 0;
B obj(_int);
A *obj2 = &obj;
_int++;
...
}

问题是 _int 正在更改为 1,但是m_int 保持不变。我需要 m_int 来反映 _int 中的值,而不需要用代码更新它。我认为有一个指向内存位置的指针会起作用吗?

第二个问题是,当我将鼠标悬停在 obj2 上以查看 obj 的值时,我收到消息“无法评估子项”。

I have these classes:

class A
{
};

class B : public A
{
public:
    B(int val);
private:
    int* m_int;
};

B::B(int val)
{
    m_int = &val;
}

I call the code like so, inside 1 function, i know the vars will be destroyed once they are out of scope, but I only need them in this function:

{
...
int _int = 0;
B obj(_int);
A *obj2 = &obj;
_int++;
...
}

The problem is that _int is changing to 1, but m_int stays the same. I need m_int to reflect what the value in _int is, without the need to update it with code. I thought having a pointer to a memory location would work?

The second problem is that when I hover my mouse over obj2, to see the values of obj, I get the message "children could not be evaluated".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

献世佛 2024-11-14 04:12:58

我不太确定以下内容对新手来说是否是个好建议,但使用这样的参考是可行的:

class B : A
{
public:
    explicit B(int& val);
private:
    int* m_int;
};

B::B(int& val)
{
    m_int = &val;
}

我有点担心这实际上是糟糕设计的标志。如果只是为了学习目的,那就继续吧,理解总是很重要的

I'm not too sure whether the following is good advice for a novice, but using a reference like this would work:

class B : A
{
public:
    explicit B(int& val);
private:
    int* m_int;
};

B::B(int& val)
{
    m_int = &val;
}

I'm a bit worried that it is actually a sign of bad design. If it is just for learning purposes, go right ahead, understanding is always important

动次打次papapa 2024-11-14 04:12:58

指针m_int将保持不变,但指向*m_int将会改变,您还必须传递值val 通过引用函数。

编辑:看到您编辑的问题,因为这是从构造函数完成的,您可以这样做:

class B : A
{
public:
    B(int& val) : m_int(val) {}
private:
    int& m_int;
};

m_int引用相同的变量。

The pointer m_int will stay the same but the value pointed to *m_int will change, you'll also have to pass the value val by reference to the function.

EDIT: Saw your edited question, since this is done from the constructor you can do:

class B : A
{
public:
    B(int& val) : m_int(val) {}
private:
    int& m_int;
};

To have m_int reference the same variable.

终弃我 2024-11-14 04:12:58

如果您想更改 _int 的值,您应该传递它的指针,您现在所做的方式是按值调用。

B::B(int* val)
{
    m_int = val; // not ref
}

并且 m_int 应该是 int* 类型而不是 int

对于第二个问题,您将派生类的值存储在指向基类的指针中,因此当您尝试查看是什么时在 obj2 内部,您只能看到基类型的部分,这就是为什么您收到消息children无法评估的原因。

编辑--
如果您想查看 obj2 中的实际内容,您应该再次将其转换为派生类型。而且你可能并不总是知道在运行时......

If you want to change value of _int you should pass it's pointer, they way you're doing now is call by value.

B::B(int* val)
{
    m_int = val; // not ref
}

and m_int should be of type int* not int

For second problem, you're storing a value of derived class in a pointer to base, So when you try to see what's inside obj2 you only see the part of type base, that's why you get message children cannot be evaluated.
Edit--
If you want to see what actually is in obj2, you should cast it to derived type again. And you may not always know that at runtime...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文