简体   繁体   中英

Passing a class as argument which has a private constructor that takes no parameters

Simulator is supposed to carry a copy of Missile object inside it. But Missile has no public constructor with zero parameters, so I'm not able to pass it as parameter.

I'm using Visual Studio 2010, and it gives the following errors:

Error 1 c:...\simulator.cpp Line #5 error C2248: 'Missile::Missile': cannot access private member declared in class 'Missile'

Error 2 c:...\simulator.cpp Line #4 IntelliSense: "Missile::Missile()" (declared at line 11 of "c:...\Missile.h") is inaccessible

So, how do I pass object of a class who has private constructor as argument?

(Note: My code has no entry point. I'm just trying to compile this as a library.)

Missile.h

#ifndef MISSILE_H
#define MISSILE_H

class Missile
{
    public:
        Missile(double xm0, double ym0, double Vmx0, double Vmy0);
        ~Missile();

    private:
        Missile();                      // Line #11
};

#endif

Missile.cpp

#include "Missile.h"

Missile::Missile(double xm0, double ym0, double Vmx0, double Vmy0)
{
}

Missile::Missile()
{
}

Missile::~Missile()
{
}

Simulator.h

#ifndef SIMULATOR_H
#define SIMULATOR_H

#include "Missile.h"

class Simulator
{
    public:
        Simulator(const Missile & missile);
        ~Simulator();

    private:
        Missile m_Missile;
};

#endif

Simulator.cpp

#include "Simulator.h"

Simulator::Simulator(const Missile & missile)
{                                       // Line #4
    m_Missile = missile;                    // Line #5
}

Simulator::~Simulator()
{
}

You would use what's called a ctor-initializer (some people call it an "initializer list", although the C++ standard actually calls it a "ctor-initializer"):

// In your Simulator.cpp file:
Simulator::Simulator(const Missile & missle) : m_Missile(missle)   
                                            /*^^^^^^^^^^^^^^^^*/
                                            /* This part here */
{
    /* No assignment needed */
}

This is equivalent to calling the copy constructor of the Missile class, which is automatically generated for you by the compiler, and all it does is copy each data member:

class Missile
{
public:
    Missile(double xm0, double ym0, double Vmx0, double Vmy0);
    ~Missile();

    // Compiler generated copy constructor
    Missile(const Missile& rhs) /* copies each data member */
    {
    }

    // Compiler generated copy assignment operator
    Missile& operator=(const Missile& rhs)
    {
        /* copies each data member */
    } 

private:
    Missile(); // default constructor
};

Note that the Simulator class would also have the compiler-generated copy constructor and the copy assignment operator as well.

Without the c-tor initializer , the Missile member would be default initialized instead, which would call the default constructor, which you have declared to be private .

You should always use c-tor initializer s when you can , since it is much more general than assigning to member variables in the constructor.

This is actually a rather fundamental concept in C++, and any good book on C++ would cover constructors and compiler-generated functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM