对象初始化之前在对象上运行的方法? [英] method running on an object BEFORE the object has been initialised?

查看:82
本文介绍了对象初始化之前在对象上运行的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

class Foo
{

public:

 Foo(): initialised(0)
 {
  cout << "Foo() gets called AFTER test() ?!" << endl;
 };

 Foo test()
 {
  cout << "initialised= " << initialised << " ?! - ";
  cout << "but I expect it to be 0 from the 'initialised(0)' initialiser on Foo()" << endl;
  cout << "this method test() is clearly working on an uninitialised object ?!" << endl;
  return Foo();
 }

 ~Foo()
 {};

private:

 int initialised;

};


int main()
{

 //SURE this is bad coding but it compiles and runs
 //I want my class to DETECT and THROW an error to prevent this type of coding
 //in other words how to catch it at run time and throw "not initialised" or something

 Foo foo=foo.test();

}

解决方案

您不能阻止人们的编码不正确.它就像应该"一样工作:

  1. 为Foo分配内存(这是"this"指针的值)
  2. 通过以下操作进入Foo :: test:Foo :: test(this),其中,
  3. 它通过this-> initialized来获取值,它是随机的垃圾,然后它
  4. 调用Foo的默认构造函数(由于返回Foo();),然后
  5. 调用Foo的copy构造函数,以复制右手的Foo().

就像应该的那样.您不能阻止人们不知道使用C ++的正确方法.

您能做的最好的就是拥有一个魔术数字:

class A
{
public:
    A(void) :
    _magicFlag(1337)
    {
    }

    void some_method(void)
    {
        assert (_magicFlag == 1337); /* make sure the constructor has been called */
    }

private:
    unsigned _magicFlag;
}

之所以有效",是因为_magicFlag的机会已分配到已经为1337的位置的可能性很小.

但实际上,不要这样做.

#include <iostream>
using namespace std;

class Foo
{

public:

 Foo(): initialised(0)
 {
  cout << "Foo() gets called AFTER test() ?!" << endl;
 };

 Foo test()
 {
  cout << "initialised= " << initialised << " ?! - ";
  cout << "but I expect it to be 0 from the 'initialised(0)' initialiser on Foo()" << endl;
  cout << "this method test() is clearly working on an uninitialised object ?!" << endl;
  return Foo();
 }

 ~Foo()
 {};

private:

 int initialised;

};


int main()
{

 //SURE this is bad coding but it compiles and runs
 //I want my class to DETECT and THROW an error to prevent this type of coding
 //in other words how to catch it at run time and throw "not initialised" or something

 Foo foo=foo.test();

}

解决方案

You can't prevent people from coding poorly, really. It works just like it "should":

  1. Allocate memory for Foo (which is the value of the "this" pointer)
  2. Going to Foo::test by doing: Foo::test(this), in which,
  3. It gets the value by this->initialised, which is random junk, then it
  4. Calls Foo's default constructor (because of return Foo();), then
  5. Call Foo's copy constructor, to copy the right-handed Foo().

Just like it should. You can't prevent people from not knowing the right way to use C++.

The best you could do is have a magic number:

class A
{
public:
    A(void) :
    _magicFlag(1337)
    {
    }

    void some_method(void)
    {
        assert (_magicFlag == 1337); /* make sure the constructor has been called */
    }

private:
    unsigned _magicFlag;
}

This "works" because the chances _magicFlag gets allocated where the value is already 1337 is low.

But really, don't do this.

这篇关于对象初始化之前在对象上运行的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆