简体   繁体   English

如何将名称传递给基类的构造函数

[英]c++ How to pass a name to the constructor of base class

I have two classes and I want every object of subclass to have a name, which is passed as an argument to the constructor of superclass . 我有两个类,并且我希望子类的每个对象都有一个名称,该名称作为参数传递给superclass的构造函数。 How do I do this? 我该怎么做呢?

class base
{
   public:
     base(const char * name) {name = name;}
     const char * getName() {return name;};
   private:
     const char * name;
};
class derived : public base
{
   public:
     derived(const char * name) : base(name) {}
};

However, 然而,

int main(int argc, char **argv)
{
    derived d("test");
    std::cout << d.getName();
}

produces gibberish as the the console output. 产生乱码作为控制台输出。

You need to choose better name for parameter name : 您需要为参数name选择更好的name

base(const char * p_name) {name = p_name;}

in you code you are just assigning name to itself. 在代码中,您只是为其本身分配名称。

Other possible solutions: 其他可能的解决方案:

base(const char * name) : name(name) {} // preferred one
base(const char * name) {base::name = name;}
base(const char * name) {this->name = name;}

The problem occurs because you're assigning name to itself. 发生问题是因为您要为其分配name So the member variable const char * name won't have the correct value. 因此,成员变量const char * name将没有正确的值。 Since this is C++ though you should really use std::string : 由于这是C ++,尽管您应该真正使用std::string

#include <string>
class base
{
   public:
     base(std::string name) {m_name = name;}
     std::string getName() {return m_name;};
   private:
     std::string m_name;
};
class derived : public base
{
   public:
     derived(std::string name) : base(name) {}
};

Your problem is the way you use name in the body of the constructor, where it is local parameter. 您的问题是您在构造函数主体中使用name的方式,该name是局部参数。 To fix the code, either use initialization lists or refer to member name through this . 要修复代码,请使用初始化列表或通过this引用成员名称。

Example 1 (member list, preferred!): 示例1(成员列表,首选!):

base(const char * name) : name(name) {}

Example 2 (use this , not preferred): 示例2(使用this ,不是首选):

base(const char * name) : { this->name = name; }
  • name is used twice in the constructor, you could change the member to name_ for example: name在构造函数中使用了两次,您可以将成员更改为name_ ,例如:

     class base { public: base(const char * name) {name_ = name;} const char * getName() {return name_;}; private: const char * name_; }; 

    or use a initialization list: 或使用初始化列表:

      base(const char * name) : name(name) {} 

    if you want to keep the same name for the parameter as well as for the member variable. 如果要为参数和成员变量保持相同的名称。

  • In C++, you can use std::string instead of a char* for the name. 在C ++中,您可以使用std::string代替char*作为名称。

  • In class base , no memory is allocated for name (in this case it would be better to copy the data with strcpy() instead of keeping a pointer to a constant parameter). class base ,没有为name分配内存(在这种情况下,最好使用strcpy()复制数据,而不要保留指向常量参数的指针)。

You have a problem with ambiguous use of the name symbol. 您对name符号的含糊使用存在问题。

The obvious answer is: 显而易见的答案是:

Make the symbols of parameter and member variable unambiguous. 使参数和成员变量的符号明确。


Your code can be fixed like this: 您的代码可以像这样固定:

#include <iostream>

class base
{
   public:
     base(const char * name) {name_ = name;}
     const char * getName() {return name_;};
   private:
     const char * name_; // <<< Just make the member variable unambiguous.
};
class derived : public base
{
   public:
     derived(const char * name) : base(name) {}
};

int main(int argc, char **argv)
{
    derived d("test");
    std::cout << d.getName() << std::endl;
}

See the working demo please. 请参阅工作演示

base(const char * name) {name = name;}

is not doing what you are expecting. 没有按照您的期望做。 In the function, the argument name shadows the member variable name . 在函数中,参数name遮盖了成员变量name Hence, the member variable never gets initialized. 因此,成员变量永远不会初始化。

You can use: 您可以使用:

base(const char * name) : name(name) {}

or better 或更好

base(const char * nameIn) : name(nameIn) {}

Suggestion for further improvement: 进一步改进的建议:

Use a std::string for the member variable. 使用std::string作为成员变量。 Then your class doesn't have to deal with problems that might arise if the pointer becomes a dangling pointer after the constructor is called. 这样,您的类就不必处理如果在构造函数被调用后指针变为悬空指针时可能出现的问题。

class base
{
   public:
     base(const char * nameIn) : name(nameIn) {}
     std::string const& getName() {return name;} const;
   private:
     std::string name;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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