调用类构造函数作为方法 [英] call class constructor as a method

查看:150
本文介绍了调用类构造函数作为方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个班, TournamentMember 玩家其中玩家源自 TournamentMember 。每个类都有参数构造函数。



TournamentMember 有这个构造函数:

  TournamentMember(const char * name,std :: string country,int height);类具有此构造函数:        p> 

  Player(int number,int goal,std :: string position,std :: string feet) 

我从 Player 类创建一个对象像这样:

 玩家soccer_player(40,34,守门员,右); 

每位足球运动员应该有7个属性,分别是姓名,国家,身高,脚。这些属性中的4个是从玩家soccer_player(40,34,守门员,右); 中设置,左边3应该从类TournamentMember中分配。我怎么做?我知道一种方法是从方法(set方法),但我想用构造函数,可能sth这样:(我知道以下示例是错误的)

  soccer_player.TournamentMember(John,USA,170); 

谢谢

解决方案

在我看来,你需要使用最差的教学之一,在所有C ++的东西。成员初始化列表!



为了满足 TournamentMember 玩家的构造函数需要更多的语法,初始化器列表和需要传递到 TournamentMember 的变量。

  Player(int number,
int goal,
std :: string position,
std :: string feet,
const char * name,
std :: string country,
int height):TournamentMember(name,country,height)
{
// constructor body
//这里设置的任何成员变量可以和初始化器列表中的TournamentMember
//一起设置。通常这意味着构造函数的主体是
//完全为空。
}

冒号后面的代码是成员初始化列表。这允许您在之前调用成员变量或基类构造函数执行类的构造函数的主体。



如果您要初始化基类,请在此处执行。



成员变量必须在进入构造函数体之前构造,以便它们可以使用。通常使用默认构造函数。如果你有一个没有默认构造函数的成员变量,使用初始化器列表。



如果你有一个复杂的成员变量,你需要初始化,默认构造,创建一个临时,然后将临时复制到成员变量,例如预加载向量或对象与计算密集的构造逻辑,你不想重复,这里是做它的地方。



此外,编译器在使用初始化器列表时通常还会进行优化



进一步阅读: http://en.cppreference.com/w/cpp/language/initializer_list p>

I have 2 classes, TournamentMember and Player where Player is derived from TournamentMember. Each of the classes has parametric constructor.

TournamentMember has this constructor:

TournamentMember(const char* name, std::string country, int height); 

Player class has this constructor:

Player(int number, int goals, std::string position, std::string feet);

I create an object from the Player class like this:

Player soccer_player(40, 34, "goalkeeper", "right");

Each soccer player should have 7 properties which are name, country, height, number, goals, position, feet. 4 of those properties are set from Player soccer_player(40, 34, "goalkeeper", "right"); and the left 3 should be assigned from the class TournamentMember. How do I do that? I know a way is from methods (set methods) but I would like to do it with constructors, possibly sth like this: (I know the example below is wrong)

soccer_player.TournamentMember("John", "USA", 170);

Thank you

解决方案

You need to use one of the most poorly taught, in my opinion, things in all C++. The member initialization list!

In order to satisfy TournamentMember, Player's constructor needs to pick up a bit more syntax, the initializer list, and the variables that need to be passed into TournamentMember.

Player(int number, 
       int goals, 
       std::string position, 
       std::string feet,
       const char* name, 
       std::string country, 
       int height) : TournamentMember(name, country, height)
{
    // constructor body
    // any member variables set here can be set along with TournamentMember
    // in the initializer list. Often this means the constructor's body is 
    // completely empty.
}

The code after the colon is the member initialization list. This allows you to call a member variable or base class constructor before executing the body of the class's constructor.

If you want to initialize a base class, do it here.

Member variables must be constructed before entering the body of the constructor so that they are ready for use. Typically the default constructor is used. If you have a member variable without a default constructor, use the initializer list.

If you have a complex member variable you need to initialize and don't want to have to default construct, create a temporary, and then copy the temporary into the member variable, for example pre-loading a vector or an object with computationally intensive construction logic you'd rather not repeat, here's the place to do it.

In addition there are often optimizations the compiler can apply when using the initializer list

Further reading: http://en.cppreference.com/w/cpp/language/initializer_list

这篇关于调用类构造函数作为方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆