const     SimpleCat * const FunctionTwo (const SimpleCat * const theCat);

Ok so this is a function declaration. I'll pose my questions numbered so it is easier. I'm taking an online course in C++ but got stumped on this line of code.

1. The return type is of type SimpleCat and is a pointer. Why do I need to 'return' a pointer? The author of the code writes in FunctionTwo "return theCat;" Why did he do that?

2. I'll make a few statements, please correct me if I am wrong.

The first const says the returned pointer is constant??

The second const says FunctionTwo is a constant function and cannot change any values? I thought the syntax was for the const keyword to be after the function declaration before the terminator.

The third const says the pointer is const to the address given to the function and cannot be reassigned.

The fourth const says the object theCat(which is pointing to whatever) is const therefore only const member methods can be called. Can const member variables be called?

Thanks in advance.

Recommended Answers

All 3 Replies

> Why do I need to 'return' a pointer?
Probably because returning a full object is too expensive. Pointers are usually smaller than the objects they point to, so passing and returning pointers can be more streamlined.

> The author of the code writes in FunctionTwo "return theCat;" Why did he do that?
Edward usually does that as a convenient way to "filter" an object: Pass in an object--by pointer, because it's cheaper--make some changes, and return the changed object. The author of your code passes in a fully const pointer--both the pointer and object being pointed to are const--so the filter option isn't possible. It's probably just a convenient way to use theCat in the same statement as the function call:

std::cout << FunctionTwo(theCat)->Name << '\n';

This calls FunctionTwo and prints the name of the cat all in one statement.

> The first const says the returned pointer is constant??
The first const--or rather the const before the asterisk--says that the object being pointed to is read-only. You can't directly change any of the data members that aren't explicitly declared as mutable.

> The second const says FunctionTwo is a constant function and cannot change any values?
The second const--the const after the asterisk--says that the pointer itself is read-only. You can't directly assign a different address to it.

> The third const
The third const is analogous to the first const except it applies to the parameter instead of the return value.

> The fourth const
The fourth const is analogous to the second const except it applies to the parameter instead of the return value.

the second and fourth const are utterly and completely pointless. there is never a point to declare that a value passed by value is not going to be modified. you should remove them

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.