Izzo
Izzo

Reputation: 4928

C++ Passing Const Object Reference Into Constructor

I am currently designing a class that will reference the data of another object. In this new class, I am attempting to pass an object reference such that the new class can view values of the object without having to copy the original data of the object. However, I am struggling with this.

I currently have the following class prototype:

Force2Motion(const RigidBody& rigid_body);

So when I pass this reference into the constructor, I would like to save this for future use after the object has been initialized. Thus I want some way to say

RigidBody _memberObject = rigid_body;

However, I want to avoid copying all of rigid_body into its own object. Instead, I was thinking that it would be more appropriate to do something like this

// Force2Motion Member Object
RigidBody& _rigidBody;

And then during object creation have the following constructor

Force2Motion(const RigidBody& rigid_body):
    _rigidBody(rigid_body)

However, this throws an error saying that I am not allowed to assign a reference to a const reference. I want to make sure that this new class does not reassign the constructor reference, thus I believe I need the const.

I did realize that if I were to initialize the member object as a const reference it would allow me to run the constructor fine, but then I couldn't use any of the objects member functions because the compiler thought that accessing the member function could possibly change the data of the original object reference.

Such that I have:

const RigidBody& _rigidBody;

with constructor

 Force2Motion(const RigidBody& rigid_body):
      _rigidBody(rigid_body)

Are there changes that can be made to the RigidBody class such that I can still use member functions even if I have declared an object as const? As in, can I mark certain functions such that they indicate accessing them will not change any data within the object?

Thanks for any help!

Edit: In my process of describing the problem, I realized the real question in which I should have been asking. Thus I was easily able to search for the question. Check out this link. It explains that if you initialize an object as a const, you will not be able to access any member function of that object since accessing member functions can possibly change data within the object that was defined as a const. Thus, if you have a member function that you KNOW will not change the data of the object, you need to mark that function with a const keyword (refer to the link for specifics). Doing so will allow you to declare an object as const yet still have access to certain member functions.

Upvotes: 1

Views: 1388

Answers (1)

Michael Albers
Michael Albers

Reputation: 3779

Yes you can. Member functions can be made const

void myFunction() const;

Using this const makes the this pointer const, thus meaning you cannot modify member variables or call other non-const member functions. There are some exceptions to this, like const_cast and the mutable keyword. But those should probably be avoided.

A good, simple example of when to use this const is on getters.

int getSize() const
{
   return size;
}

Since nothing will change, this function can be called on a const or non-const object.

Upvotes: 2

Related Questions