简体   繁体   English

C ++中的Setter getter方法

[英]Setter getter methods in C++

I'm new to c++. 我是C ++的新手。 I created following classes Into, MyWstring as follows: I tried to create setter for an object variable as setInto method. 我按如下方式创建了以下类Into,MyWstring:我试图为对象变量创建setter作为setInto方法。 It complains no such constructor Into(). 它没有抱怨这样的构造函数Into()。 What should I do? 我该怎么办? How to create setter for this? 如何为此创建二传手? (Basically what is my expectation is how to achieve Java like setters in C++) (基本上我的期望是如何像C ++中的setter一样实现Java)

Into.h 进入

#ifndef INTO_H_
#define INTO_H_

class Into {
public:
    Into(int id1);
    virtual ~Into();

    int id;
};

#endif /* INTO_H_ */

Into.cpp 进入cpp

#include "Into.h"

Into::Into(int id1) {
    // TODO Auto-generated constructor stub
    id = id1;
}

Into::~Into() {
    // TODO Auto-generated destructor stub
}

MyWstring.h MyWstring.h

#ifndef MYWSTRING_H_
#define MYWSTRING_H_

#include<iostream>
#include"Into.h"

using namespace std;

class MyWstring {
public:
    MyWstring(wstring test1);
    virtual ~MyWstring();
    void assign(MyWstring m);
    void setInto(Into into1);

    wstring test;
    Into into;
};

#endif /* MYWSTRING_H_ */

MyWstring.cpp MyWstring.cpp

#include "MyWstring.h"

MyWstring::MyWstring(wstring test1) {
    test = test1;
}

MyWstring::~MyWstring() {
    // TODO Auto-generated destructor stub
}

void MyWstring::assign(MyWstring m)
{
    m.test = L"M";
}

void MyWstring::setInto(Into into1)
{
    into = into1;
}

Your class has an instance variable into that has no default constructor (one without arguments). 你的类有一个实例变量into一个没有默认构造函数(没有一个参数)。

When MyWstring is created, it needs to create an instance of Into , but cannot do so because it does not know how to. 创建MyWstring ,它需要创建Into的实例,但是不能这样做,因为它不知道如何做。

Solution 1: Give Into a default constructor 方案1:Into一个默认的构造函数

class Into {
    [...]
    Into() : id(0) { }
};

Solution 2: Mention into in the initializer list for MyWstring : 方案2:提及into在初始化列表MyWstring

MyWstring::MyWstring(wstring test1) 
     : test(test1), into(0)
{
}

Note the additional change of assigning test in the initializer list. 请注意在初始化列表中分配test的其他更改。 Otherwise it gets default-constructed and then copy-assigned, which is probably not what you want. 否则,它将默认构造,然后进行复制分配,这可能不是您想要的。

If into does not have a sensible default value, you might need to re-think your logic and use a pointer to an Into object instead (but make sure to use std::unique_ptr<> or similar). 如果into没有合理的默认值,则可能需要重新考虑逻辑并改用指向Into对象的指针(但请确保使用std::unique_ptr<>或类似方法)。

When you construct a MyWString , the compiler will call the constructors of all base classes (you don't have any), and sub-objects. 当构造MyWString ,编译器将调用所有基类(您没有任何基类)和子对象的构造函数。 If you don't provide an argument, it will call the constructor without arguments - and you don't have one. 如果不提供参数,它将调用不带参数的构造函数-并且您将没有参数。 Your choices are: 您的选择是:

Provide a default constructor: 提供默认的构造函数:

....
Into(int id1);
Into();
...

Into::Into() : id(0) {}  // Always prefer to initialize rather than assign later

Initialize MyWString::into : 初始化MyWString::into

MyWstring::MyWstring(wstring test1)
   : test(test1)
   , into(0) 
{}

Try changing your MyWstring c'tor definition to this: 尝试将MyWstring c'tor定义更改为此:

    MyWstring::MyWstring(wstring test1)
    :
    into( 0 ),
    test( test1 )
    {

    }

You have a variable of type Into in class MyWstring, which does not have a default c'tor and hence compiler can not instantiate it by itself. 您在类MyWstring中有一个Into类型的变量,它没有默认的c'tor,因此编译器无法单独实例化它。

Also it is better if your MyWString class accepts the value for the variable "into" in c'tor so that you have an actual value to set, instead of setting some default value. 如果MyWString类在c'tor中接受变量“ into”的值,以便您可以设置一个实际值,而不是设置一些默认值,则更好。

The compiler is complaining that you don't have a default constructor. 编译器抱怨您没有默认的构造函数。 The default constructor is called, When you construct an object without passing any arguments, eg 当构造一个不传递任何参数的对象时,例如,默认构造函数称为

Into into;

The default constructor is also automatically called if your class is member of another class. 如果您的类是另一个类的成员,则也会自动调用默认构造函数。

The default constructor is auto-generated if there is no user-declared constructor. 如果没有用户声明的构造函数,则会自动生成默认的构造函数。 In your case you have the constructor Into(int id1) which prevents the compiler from auto-generating a default constructor. 在您的情况下,您可以使用构造函数Into(int id1)来防止编译器自动生成默认构造函数。

So what you need is one of the three lines below 因此,您需要的是以下三行之一

Into():id(0){};
Into() = default; // C++11 syntax
Into():Into(0){}; // call another constructor from your constructor.

Alternatively, if you have a constructor where all its arguments have default values, the compiler will use that as the default constructor. 另外,如果您有一个构造函数,其所有参数均具有默认值,则编译器会将其用作默认构造函数。 So you can also do like this 所以你也可以这样

Into(int _id = 0) : id(_id){};

If you don't want a default constructor for your class then you need to call the non-default one on the constructor of your other class. 如果您不希望为类使用默认构造函数,则需要在其他类的构造函数上调用非默认构造函数。

MyWstring::MyWstring(wstring test1): test(test1), into(0) 
{}

Now to the next part, you are assigning an object of type Into to another object of the same type which means that the copy assignment operator is being used here. 现在到了下一个部分,要指定类型的对象Into到同一个类型,这意味着拷贝赋值运算符正在这里使用的另一个对象。 You are in luck because in your case the compiler auto-generates the copy assignment operator. 您很幸运,因为在您的情况下,编译器会自动生成副本分配运算符。

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

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