• Advertisement
Sign in to follow this  

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

This topic is 4589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 = &example1;
const Layer & example4 = example1;

example1.getWeights(); //works without the tailing const
example2.getWeights(); //needs the tailing const to compile
example3->getWeights(); //needs the tailing const to compile
example4.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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement