Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++, Question on function returning a const float** variable?

Started by johnnyBravo Jul 31, 2005 at 8:45 AM 6 replies 1.3k views
Original Post
johnnyBravo
johnnyBravo
Hi im using c++, I've got a function in my class that is supposed to return a two dimensional array. eg:

class Layer {
private:
	float **weights;
public:
	const float **Layer::getWeights() {
		return weights;
	}

I'm returning const, as because so the weights can't be changed from outside the class(class design reasons). But I get this error when I try it:
Quote:
c:\my\new\nnback\layer.cpp(122) : error C2440: 'return' : cannot convert from 'float **' to 'const float **' Conversion loses qualifiers
When I have a one dimensional pointer array, it works fine, but why can't I do it with a two dimensional pointer array, and what are the possible solutions for this problem? Thank you.
EliasAE
EliasAE
Maybe you mean:
	const float * const * Layer::getWeights() const {
johnnyBravo
johnnyBravo
Quote:
Original post by EliasAE
Maybe you mean:
	const float * const * Layer::getWeights() const {


What does the const at the very end do?

as in Zahlman's link, it just uses: void g(const Foo* const* p);

Thanks alot.
MaulingMonkey
MaulingMonkey
Quote:
Original post by johnnyBravo
Quote:
Original post by EliasAE
Maybe you mean:
	const float * const * Layer::getWeights() const {


What does the const at the very end do?


This allows getWeights to be called on a constant layer:
Layer example1;const Layer example2;const Layer * example3 = &example1const Layer & example4 = example1;example1.getWeights(); //works without the tailing constexample2.getWeights(); //needs the tailing const to compileexample3->getWeights(); //needs the tailing const to compileexample4.getWeights(); //needs the tailing const to compile

Note that this implitly makes "this" (and thus it's members) constant as well in the implementation. This will fail to compile:
class foo {    int bar;public:    void baz() const {        bar = 3; //error: attempting to reassign a "const" variable    }};

Very occasionally it makes sense to allow a member variable to be modified through a "const" function (e.g. reference counting). The solution to this is to use the mutable keyword:
class foo {    mutable int bar;public:    void baz() const {        bar = 3; //OK, bar is explicitly mutable    }};

Note that you can still change items referenced/pointed at from a const function:
class foo {    int & referece;    int * pointer;public:    foo ( ... ) : ... { ... }    void baz() const {        reference = 13; //OK, we're changing what's referenced, not the reference itself.        *pointer = 13; //OK, we're changing what's being pointed at, not the pointer itself.        pointer = & some_other_int; //error: the pointer itself is constant (we cannot change where it points)    }};

This adventure in const-correctness and it's implications brought to you by the number 9 and the letter M.
Zahlman
Zahlman
You know, you could have taken half a second to notice that my link was to an anchor near the bottom of a page all about const correctness, which included an answer to your follow-up question :( Jeez, you can't even spoon-feed people these days.
MaulingMonkey
MaulingMonkey
Geesh, I shoulda linked that instead of writing (another) explaination :-p.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.