English 中文(简体)
c ++ 在建造自建期间通过 *
原标题:c++ Passing *this as reference during construction of self

Is the following safe? I know, strictly speaking, dereferencing a pointer before the thing to which it points has been properly constructed seems dangerous, but I imagine the compiler will just provide a pointer without actually doing any dereferencing. Well, I assume.

(Note: gInst doesn t actually use the reference until much later.)

TU 1

Sys::System::System::System(const sf::VideoMode& rVM, const std::string& rSTR, unsigned long settings) :
    win(rVM, rSTR, settings),
    gInst(*this)
{

}

Header A

namespace Sys
{
    namespace System
    {
        struct System;
    }

    namespace UI
    {
        struct GUI
        {
            System::System& topl;
            MenuBar menu;

            GUI(System::System&);

            private:

            GUI();
            GUI(const GUI&);
            GUI& operator=(const GUI&);
        };
    }
}

标题B

namespace Sys
{
    namespace System
    {
        struct System
        {
            sf::RenderWindow win;
            Sys::UI::GUI gInst;
            Sys::Editor::Editor eInst;

            System(const sf::VideoMode&, const std::string&, unsigned long);

            private:

            System();
            System(const System&);
            System& operator=(const System&);
        };

        void start();
    }
}
最佳回答

(说明:gInst don t 直到较晚才实际使用该参考。)

Then it s completely safe. (Taking note you say "the reference", not "the value".)

Compilers will just warn about it for the reason you ve stated, but there s nothing undefined about it, just "risky". Note that you can often trump compiler warnings (if they bother you) with something like this:

struct foo
{
    foo() : b(self())
    {}

private:
    foo& self() { return *this; }

    bar b;
};
问题回答

As long as gInst never accesses a data member or member function of gInst or attempts to perform run-time type introspection via RTTI or dynamic_cast, until after the constructor has completed, then it is safe.

The short answer: Maybe, if you re careful.

The long answer: https://isocpp.org/wiki/faq/ctors#using-this-in-ctors

Relating to your specific issue: That s fine, as long as gInst really is a reference.

(a class called System in the namespace System in the namespace Sys? o.O)

How is gInst declared? if it takes a reference and stores it as reference(Sys::System::System &)/pointer(Sys::System::System *) its safe. If it takes a raw class (Sys::System::System) or stores it as a raw class, it will not be safe as it will copy it.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签