• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

[C++] Initilizing a member in the header file

Vitus

Resource contributor
Messages
1,480
Country
newzealand
I'm having a really dumb moment here. Let's say I have a class "Bar" defined with a constructor as follows:
C++:
class Bar{
private:
    double _value;
public:
    Bar(double value);
}
An instance of this class is member of another class called "Foo":
C++:
class Foo{
private:
    Bar bar;
}

Is there a way to call the custom constructor Bar(double value) without using new? Preferably, I'd like to have the value initialized in the header file of Foo:
C++:
class Foo{
private:
    Bar bar(42.0);
}

But this doesn't seem to be working....
 
I not expert in this code programming, still student from @JB3DG, I try to give a help

C++:
Bar bar;
Foo foo;

class Bar
{
public:
    Bar() { };
    ~Bar() { };

    int var1;
    int var2;

    void Function();
private:

};

class Foo
{
public:
    Foo() { };
    ~Foo() { };

    // identical var from class Bar
    int var1;
    int var2;

    // new var
    int var3;
    int var4;


private:

};


void Bar::Function()
{
    // I use 4 variable from 2 class
    // sample
    var1 = foo.var3;
    foo.var4 = var2 + var1;

    // I usually do this,
    // this function is different var from above.
    bar.var1 = foo.var3;
    foo.var4 = bar.var2 + bar.var1;
    // see the different?

    // you can use identical "var" on different class, but
    // always include class pointer

    foo.var1 = 2 * bar.var2;
    // you can do mixed between class, always remember the pointer
    // etc, etc
}
 
Last edited:
@cheangjc gave the answer I was going to give. I normally do the constructor body inside the class definition in the header file rather than merely declaring it in the class and adding the body in a cpp file. I do however, define my function bodies and global variables in the cpp file. Headers are otherwise only for cross referencing with other cpps.
 
Oh man, thanks! I feel so dumb once again :oops:

I also found another solution, but I don't know if there's any downsides to it:
Code:
class Foo{
private:
    Bar bar = Bar(42.0);
}

Funny enough, this seems to be working, and since there's no new, I take it that there's no need to delete it. What's nice about this is that I don't need to define a constructor for Foo in this case.

I try to keep my header files clean of ANY implementations, other than some initialization values.
 
I would confirm that bar is being deleted by the code. I have learned through the years to always check, never assume... especially with regards to Microsoft code.
 
Back
Top