Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Learning C++ const reference

Name: Anonymous 2011-12-10 11:09

Hi, guys... I'm trying to learn C++ and OpenGL...

I want to return the first pointer of the array... but maintaing the const correctness...


it happened that:

class Foo{
private:
    GLint array[64][64][4];
public:
    const GLint& get_array(){return array;}
}


gives me this compiler error:


/Users/Pella/Desktop/c/OpenGL_Nehe_cpp/CheckImag.h:28:0 /Users/Pella/Desktop/c/OpenGL_Nehe_cpp/CheckImag.h:28: error: invalid initialization of reference of type 'const GLubyte&' from expression of type 'GLubyte (*)[64][4]'



can you help me out in understanding the const correctness?

Name: Anonymous 2011-12-10 11:31

actually instead of

GLint


in my code I used

GLubyte

Name: Anonymous 2011-12-10 11:34

>>2
Have you ever considered posting actual working code you stupid fucker? I hate to tell you this, but some of us aren't mind readers.

Name: Anonymous 2011-12-10 11:40

>>1
What is the type of array? GLint[][][] → GLint***[/code]
What is the return type of get_array? GLint&

Name: Anonymous 2011-12-10 11:50

>>1

found the solution, but anyway thank you:

    const GLubyte*  get_array(){return (GLubyte*)array;}

probably I have expressed myself wrong...

Name: Anonymous 2011-12-10 12:05

>>5
Again, for the second time you stupid fucker, no one here really knows because you didn't post any god damn fucking working code.

Name: Anonymous 2011-12-10 12:13

>>6
I got the answer from stackoverflow... they understood what I wanted...

Name: Anonymous 2011-12-10 12:16

>>7
So? It doesn't change the fact that you're a fucking moron.

Name: Anonymous 2011-12-10 14:01

>>5

Have StackOverflow suggested that to you? No wonder that place is a crap.

First, avoid C-style casts. They can always be replaced by proper cast operators, in any situation. You'll be using static_cast. If you're forced to use reinterpret_cast or const_cast, it's a very strong sign you're doing something wrong.

Second, study a bit further the difference between a pointer and a reference. They have different syntax, and are intended to be used in different situations, albeit in the lower level they're implemented likewise. You want a pointer to the last array dimension, is that so? Your question is unclear.

Third, const correctness has seemingly nothing to do with your problem. You're having problems with the return type.

Name: Anonymous 2011-12-10 14:11

>>9
Good old C++. Introduce duplicate features to C, then declare everything wrong.

Some religion you got there, pal.

Name: Anonymous 2011-12-10 15:14

>>10
Good old trolls. Not noticing that casting unrelated things in C are just as dangerous.

Some trolling you got there, pal.

Name: Anonymous 2011-12-10 15:29

>>11
Who failed to notice it? The point is the exponential increase in lack of safety C++ duplication brings.

Don't change these.
Name: Email:
Entire Thread Thread List