Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;不使用构造函数的类声明_C++_Constructor - Fatal编程技术网

C++ C++;不使用构造函数的类声明

C++ C++;不使用构造函数的类声明,c++,constructor,C++,Constructor,我已经遇到过几次了,最后我删除了最初的构造函数。我搜索过谷歌,但我甚至不知道这叫什么 class you { private: string name; int number; COLORREF color; drinks favoriteDrink; // here public: you(string Name, int Number, COLORREF Color);

我已经遇到过几次了,最后我删除了最初的构造函数。我搜索过谷歌,但我甚至不知道这叫什么

class you {

    private:
        string name;
        int number; 
        COLORREF color;   
        drinks favoriteDrink;  // here

    public:
        you(string Name, int Number, COLORREF Color);
        string GetName();
        int GetNumber();
        COLORREF GetColor();

};

饮料是另一个类,它的构造函数看起来像:(intx,booly)。我想进一步初始化它。同样,通常我只是删除构造函数并编写一个函数ex:init(w/e)。有办法做到这一点吗?

当您定义构造函数时,它可以包括一个成员初始化列表。这是初始化成员和基类的正确方法。因此,您将实现
you
的构造函数,如下所示:

you::you(string Name, int Number, COLORREF Color) :
  name(Name),
  number(Number),
  color(Color),
  drink(whatever_arguments, you_want)
{
  // body as usual
}

请注意,这是初始化类的数据成员的唯一方法。如果您改为这样做:

you::you(string Name, int Number, COLORREF Color)
{
  name = Name;
  number = Number;
  color = Color;
}
然后你不是初始化成员,而是分配给他们。空的成员初始化器列表将首先默认初始化它们。这意味着调用类的默认构造函数,并为基元类型保留未初始化的值。然后,构造函数主体通过向它们赋值来覆盖它们


最好使用成员初始化器列表,因为您跳过了(无用的)默认初始化。有时,这也是唯一的方法,如果您有一个不可分配的成员(例如
const
成员、引用或只是一个没有可访问赋值运算符的对象)。

当您定义构造函数时,它可以包括一个成员初始值设定项列表。这是初始化成员和基类的正确方法。因此,您将实现
you
的构造函数,如下所示:

you::you(string Name, int Number, COLORREF Color) :
  name(Name),
  number(Number),
  color(Color),
  drink(whatever_arguments, you_want)
{
  // body as usual
}

请注意,这是初始化类的数据成员的唯一方法。如果您改为这样做:

you::you(string Name, int Number, COLORREF Color)
{
  name = Name;
  number = Number;
  color = Color;
}
然后你不是初始化成员,而是分配给他们。空的成员初始化器列表将首先默认初始化它们。这意味着调用类的默认构造函数,并为基元类型保留未初始化的值。然后,构造函数主体通过向它们赋值来覆盖它们


最好使用成员初始化器列表,因为您跳过了(无用的)默认初始化。有时,这也是唯一的方法,如果您有一个不可分配的成员(例如
const
成员、一个引用或只是一个没有可访问的赋值运算符的对象)。

根据您对使用new和delete的感觉,以下内容可以实现您的要求

< P > Angew的方法是最好的方法,如果你知道x和y的值足够早,如果你需要稍后再构造喜欢的饮料,你可以考虑如下的事情:

class drinks; // Forward declare class rather than #including it    

class you 
{    
private:
    string name;
    int number; 
    COLORREF color;   
    drinks* favoriteDrink;  // Constructor will not be called

public:
    you(string Name, int Number, COLORREF Color);
    ~you();
    void someFunction(void);
    string GetName();
    int GetNumber();
    COLORREF GetColor();    
};
然后在实施过程中:

#include "drinks.h" // Include it now we need it's implementation

you::you(string Name, int Number, COLORREF Color) :
  name(Name),
  number(Number),
  color(Color),
  favoriteDrink(NULL) // Important to ensure this pointer is initialised to null
{
  // body as usual
}

you::~you()
{
   delete favoriteDrink; // Ok to delete even if it was never newed, because we initialised it to NULL
   favoriteDrink = NULL; // Always set your pointer to null after delete
}

void you::someFunction(void)
{
   favoriteDrink = new drinks(3, 6) // Then once you know your values for x and y

   // Always check if the pointer is null before using
   if (favoriteDrink)
   {
      // Use the pointer
   }
}

编辑:正如Angew指出的,在C++11中有更好的管理指针的方法,如果你可以选择使用现代编译器,他的建议会使代码看起来更漂亮、更安全。

根据你对使用new和delete的感觉,以下内容可以达到你的要求

< P > Angew的方法是最好的方法,如果你知道x和y的值足够早,如果你需要稍后再构造喜欢的饮料,你可以考虑如下的事情:

class drinks; // Forward declare class rather than #including it    

class you 
{    
private:
    string name;
    int number; 
    COLORREF color;   
    drinks* favoriteDrink;  // Constructor will not be called

public:
    you(string Name, int Number, COLORREF Color);
    ~you();
    void someFunction(void);
    string GetName();
    int GetNumber();
    COLORREF GetColor();    
};
然后在实施过程中:

#include "drinks.h" // Include it now we need it's implementation

you::you(string Name, int Number, COLORREF Color) :
  name(Name),
  number(Number),
  color(Color),
  favoriteDrink(NULL) // Important to ensure this pointer is initialised to null
{
  // body as usual
}

you::~you()
{
   delete favoriteDrink; // Ok to delete even if it was never newed, because we initialised it to NULL
   favoriteDrink = NULL; // Always set your pointer to null after delete
}

void you::someFunction(void)
{
   favoriteDrink = new drinks(3, 6) // Then once you know your values for x and y

   // Always check if the pointer is null before using
   if (favoriteDrink)
   {
      // Use the pointer
   }
}

编辑:正如Angew指出的,在C++11中有更好的管理指针的方法,如果你可以选择使用现代编译器,他的建议将产生更美观、更安全的代码。

使用
std::unique\u ptr
或者更好。@Angew你是对的,这肯定会使事情更安全。我仍然停留在C++98的黑暗时代,在那里这样的东西是不存在的。我需要更好地了解最近的(!)开发
boost::scoped_ptr
或提到的
boost::optional
也应该与C++98/03一起使用。使用
std::unique_ptr
或者更好。@Angew你是对的,这肯定会使事情更安全。我仍然停留在C++98的黑暗时代,在那里这样的东西是不存在的。我需要更好地了解最近的(!)开发
boost::scoped_ptr
,或者提到的
boost::optional
也应该与C++98/03一起使用。