简体   繁体   中英

Setter getter methods in C++

I'm new to c++. I created following classes Into, MyWstring as follows: I tried to create setter for an object variable as setInto method. It complains no such constructor 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++)

Into.h

#ifndef INTO_H_
#define INTO_H_

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

    int id;
};

#endif /* INTO_H_ */

Into.cpp

#include "Into.h"

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

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

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

#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).

When MyWstring is created, it needs to create an instance of Into , but cannot do so because it does not know how to.

Solution 1: Give Into a default constructor

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

Solution 2: Mention into in the initializer list for MyWstring :

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

Note the additional change of assigning test in the initializer list. 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).

When you construct a MyWString , the compiler will call the constructors of all base classes (you don't have any), and sub-objects. 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::MyWstring(wstring test1)
   : test(test1)
   , into(0) 
{}

Try changing your MyWstring c'tor definition to this:

    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.

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.

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.

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. You are in luck because in your case the compiler auto-generates the copy assignment operator.

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