0

Maybe it's better to show the code then it is better to understand what my problem is.

Class:

Cls::Cls() {}
Cls::Cls(int &var) : m_var(var){
    std::cout << "constructor: " << m_var << std::endl;
}
Cls::~Cls() {}

void Cls::setVar() const {
    m_var = 5;
    std::cout << "setVar: " << m_var << std::endl;
}

Header:

class Cls {

public:
    Cls();
    Cls(int &var);
    virtual ~Cls();
    
    void setVar() const;
    
private:
    mutable int m_var;
};

The main:

int main() {

    int var = 1;

    Cls *cls;
    cls = new Cls(var);
    cls->setVar();
    
    std::cout << "var: " << var << std::endl;
}

So, I passed var using the custom constructor Cls(int &var). After it, I call a function changing the value of the variable. I expected, that I would see the change in the main. I was wrong. How can I achieve that? I don't want to pass the variable as function argument.

user3137385
  • 315
  • 5
  • 14
  • It should be ` mutable int &m_var`, surely? At present you're just copying the value. – user207421 Jul 12 '23 at 10:40
  • Yes you pass a reference to `var` to the constructor. But `Cls::m_var` is *not* a reference. When you initialize or assign to it, you're *copying* the value into `m_var`. – Some programmer dude Jul 12 '23 at 10:41
  • You have two variables here `var` in `main` and `m_var` in `Cls`. They are **different variables**, changing one does not change the other. – john Jul 12 '23 at 10:42
  • And why did you mark `Cls::m_var` as `mutable`? What problem is that supposed to solve? Your code doesn't deal with constant objects anywhere. – Some programmer dude Jul 12 '23 at 10:42
  • 1
    Also, there's no need for pointers here. Are you coming from a language like C# or Java, where you must use `new` to create objects? That's not needed in C++. I recommend you invest in [some good C++ beginners books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Jul 12 '23 at 10:45
  • 1
    To achieve what you want you need to change `m_var` to be a reference `int& m_var;`. But that is unlikely to be a good idea, references in classes are not a common thing to have, especially for a beginner. – john Jul 12 '23 at 10:45

3 Answers3

1
private:
    mutable int m_var;

this is not a reference, this is an int, you need:

private:
    int& m_var;

And since your class has a reference as a member, which must be initialized you cannot have default constructor.

mugiseyebrows
  • 4,138
  • 1
  • 14
  • 15
0

although you pass the var in main to Cls constructor as reference, but in Cls constructor it copies the value of parameter var to data member m_var. maybe you must declare data member m_var as:

mutable int& m_var;
CaptainHb
  • 13
  • 4
0

Ok, I found a solution. It's actually quiet easy.

Class:

Cls::Cls() {}
Cls::Cls(int &var) : m_var(&var){
    std::cout << "constructor: " << *m_var << std::endl;
}
Cls::~Cls() {}

void Cls::setVar() const {
    *m_var = 5;
    std::cout << "setVar(): " << *m_var << std::endl;
}

Header:

class Cls {

public:
    Cls();
    Cls(int &var);
    virtual ~Cls();
    
    void setVar() const;
    
private:
    int *m_var;
};

The main:

    int var = 1;

    Cls *cls;
    cls = new Cls(var);
    cls->setVar();
    std::cout << "main var 1: " << var << std::endl;
}

For those who asked why and how... This is not my actual code. This is an extremely short example showing the problem I have in my code.

user3137385
  • 315
  • 5
  • 14
  • Don't use pointers either! It's actually worse than references in a way. What is the actual and underlying problem this is supposed to solve? Please delete this question, and post a new one where you ask about the actual and underlying problem directly, instead of making it an [XY problem](https://en.wikipedia.org/wiki/XY_problem). Then we can help you solve that, with better and more appropriate solutions. – Some programmer dude Jul 12 '23 at 11:04