我需要理解this
指针概念,最好用一个例子.
我是C++的新手,所以请使用简单的语言,以便我能更好地理解它.
OK, I started using shared-pointers and pass shared-pointers as much as possible. No conversion to raw pointers anymore. This works good, except in this specific case:
Suppose we have a class that also is an observer on another class, like this:
class MyClass : public IObserver
{
public:
MyClass (std::shared_ptr<SomeOtherClass> otherClass);
void DoSomethingImportant();
private:
std::shared_ptr<SomeOtherClass> m_otherClass;
};
Run Code Online (Sandbox Code Playgroud)
This class is used like this in my application:
std::shared_ptr<MyClass> myInstance(new MyClass(otherInstance));
...
myInstance->DoSomethingImportant();
Run Code Online (Sandbox Code Playgroud)
MyClass gets a shared-pointer to another class and stores …
我正在读这篇文章" 虚方法表 "
上面的文章中的示例:
class B1 {
public:
void f0() {}
virtual void f1() {}
int int_in_b1;
};
class B2 {
public:
virtual void f2() {}
int int_in_b2;
};
class D : public B1, public B2 {
public:
void d() {}
void f2() {} // override B2::f2()
int int_in_d;
};
B2 *b2 = new B2();
D *d = new D();
Run Code Online (Sandbox Code Playgroud)
在文章中,作者介绍了对象的内存布局d
是这样的:
d:
D* d--> +0: pointer to virtual method table of D (for B1)
+4: value of int_in_b1 …
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
struct A {
private:
A* const& this_ref{this};
};
int main() {
A a{};
(void)a;
}
Run Code Online (Sandbox Code Playgroud)
如果使用-Wextra
,则GCC v6.2和clang v3.9都会显示警告.
无论如何,通过下面显示的略微修改版本,它们的行为有所不同:
struct A {
A* const& this_ref{this};
};
int main() {
A a{};
(void)a;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,GCC不会发出任何警告,clang会给出与前一个示例中返回的相同的警告.
警告几乎完全相同.
它遵循clang中的那个:
3:警告:将引用成员'this_ref'绑定到临时值[-Wdangling-field]
哪个编译器是对的?
我会说GCC在这种情况下是错误的,我正在打开一个问题,但也许这是相反的,因为该语言的一个神秘的角落案例.
以下代码将无法编译.为什么?
class A
{
int j;
void f( int i = this->j );
}
Run Code Online (Sandbox Code Playgroud)
编辑,为清楚起见.这就是我尝试做的事情,使用较少的代码行......
class A
{
void f( int i ){};
void f( );
int j;
};
void A::f()
{
f( j );
}
Run Code Online (Sandbox Code Playgroud) 我需要关于“这个”主题的可靠信息:
class MyClass, public QWidget
{
public:
MyClass( QWidget * parent = NULL )
:QWidget( parent ),
mpAnotherWidget( new QWidget( this ) ){};
private:
QWidget * mpAnotherWidget;
};
Run Code Online (Sandbox Code Playgroud)
当然,在构造函数或初始化列表中调用虚函数是一个坏主意。问题是:这个代码可以吗
mpAnotherWidget( new QWidget( this ) )
Run Code Online (Sandbox Code Playgroud)
导致未定义的行为?!如果是这样:为什么?
如果可以,请引用您的来源!谢谢!
我仍然远离掌握C#,但我的孩子正在推动我每天继续改进我的编程.
当我创建一个WinForms应用程序时,我想要实际更改并使用大量控件.
我不明白的是当我需要使用this.control
关键字时,我应该只使用control
.
示例:
如果我想更改标签的文字,我可以写
mylabel.text = "Text for label"
Run Code Online (Sandbox Code Playgroud)
要么
this.mylabel.tex = "Text for label"
Run Code Online (Sandbox Code Playgroud)
以下哪一种是正确的方法?this
在WinForms中使用控件时(例如数据网格,文本,表格等),何时使用关键字有一个简单的解释?
我对基类析构函数中的 this 指针有一个奇怪的问题。
问题描述:
我有 3 个班级:A1,A2,A3
A2从A1公开继承,从A3私下继承
class A2:private A3, public A1 {...}
Run Code Online (Sandbox Code Playgroud)
A3有一个函数getPrimaryInstance() ...返回A1类型对A2实例的引用:
A1& A3::getPrimaryInstance()const{
static A2 primary;
return primary;
}
Run Code Online (Sandbox Code Playgroud)
而A3的构造是这样的:
A3(){
getPrimaryInstance().regInst(this);
}
Run Code Online (Sandbox Code Playgroud)
(其中regInst(...)是A1中定义的函数,用于存储指向所有A3实例的指针)
类似的A3析构函数:
~A3(){
getPrimaryInstance().unregInst(this);
}
Run Code Online (Sandbox Code Playgroud)
^这就是问题发生的地方!
当名为primary的静态A2实例在程序终止时被销毁时,将调用A3析构函数,但在~A3 中,我尝试访问我正在销毁的同一实例上的函数。 =>运行时访问冲突!
所以我认为可以用一个简单的 if 语句来修复,如下所示:
~A3(){
if(this != (A3*)(A2*)&getPrimaryInstance()) //Original …
Run Code Online (Sandbox Code Playgroud) 如何让test.calculate中的函数指针赋值(可能还有其余的)工作?
#include <iostream>
class test {
int a;
int b;
int add (){
return a + b;
}
int multiply (){
return a*b;
}
public:
int calculate (char operatr, int operand1, int operand2){
int (*opPtr)() = NULL;
a = operand1;
b = operand2;
if (operatr == '+')
opPtr = this.*add;
if (operatr == '*')
opPtr = this.*multiply;
return opPtr();
}
};
int main(){
test t;
std::cout << t.calculate ('+', 2, 3);
}
Run Code Online (Sandbox Code Playgroud) 我们来看一个表示子节点树的数据结构(Node)的示例.每个对象的子节点集存储在map>中
class Node;
typedef std::shared_ptr<Node> NodePtr;
class Node
{
std::map<const std::string, NodePtr> _childNodes;
void SomeOtherMethod();
public:
bool GetChildByKeyName(/*In*/ const std::string& key, /*Out*/ NodePtr& spChild)
{
bool result = false;
auto itor = _childNodes.find(key);
if (itor != _childNodes.end())
{
spChild = itor->second;
result = true;
SomeOtherMethod();
}
return result;
}
};
Run Code Online (Sandbox Code Playgroud)
以下代码示例作为用户通常会调用它.
NodePtr spNode, spChildNode;
bool result;
...
result = spNode->GetChildByKeyName(strChildKeyName, spChildNode);
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
在我看来,调用者可能会遍历树而不必为树中的每个深度处理额外的变量
NodePtr spNode;
bool result;
result = spNode->GetChildItem(strChildKeyName, spNode);
if (result)
spNode->GetChildItem(strSubKeyName, spNode);
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,如果spNode是对象的最后剩余引用,那么我担心GetChildItem方法中的这段代码:
spChild = …
Run Code Online (Sandbox Code Playgroud) this-pointer ×10
c++ ×9
this ×4
pointers ×2
.net ×1
arguments ×1
c# ×1
clang ×1
constructor ×1
destructor ×1
dictionary ×1
gcc ×1
qt ×1
shared-ptr ×1
winforms ×1