Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const pointer as class field assign

Tags:

c++

pointers

I'm back to C++ and little help is needed. I know what's const pointer, but I can find out, how to assign it in constructor properly That's frustrating ;)

so for example:

public:
  TransferManager::TransferManager( Account * source, double amount )
  { 
    account = source; // that doesn't work ;)
  }

private:
  Account * const account;

1>proj1.cpp(63): error C2166: l-value specifies const object

That error msg isn't clear for me.

I did some research, but all I did find was difference bettwen const pointer, pointer to const var, and const pointer to const var...

like image 918
dantuch Avatar asked Aug 25 '26 20:08

dantuch


1 Answers

Use the initializer-list to initialize const members:

TransferManager::TransferManager( Account * source, double amount ) : account(source) {
}

The error message is saying that you're trying to assign to something that is const - that's not permitted. You have to initialize such member variables, not assign to them.

like image 104
Erik Avatar answered Aug 28 '26 10:08

Erik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!