Mismatch between constructor definition and declaration

I had the following C ++ code, where the argument of my constructor in the declaration had a different constant than the definition of the constructor.

//testClass.hpp
class testClass {
  public:
     testClass(const int *x);
};

//testClass.cpp
testClass::testClass(const int * const x) {}

I was able to compile this without warning using g ++, should this code compile, or at least give some warnings? It turns out that the built-in C ++ 64-bit compiler Solaris gave me a linker error, this is how I noticed that the problem was.

What is the rule for matching arguments in this case? Is this for compilers?

+5
source share
4 answers

In such cases, the const specifier is allowed to be excluded from the declaration, since it does not change anything for the caller.

. , , .

:

//Both f and g have the same signature
void f(int x);
void g(const int x);

void f(const int x)//this is allowed
{
}

void g(const int x)
{
}

f , const, .

int * const x, , . - , .

const, const int * const, , , , .

: ++, 8.3.5, 3:

" cv-, ... cv- ; "

+7

, 13.1/3b4:

, / , . , const volatile , , .

[:

typedef const int cInt;
int f (int);
int f (const int); // redeclaration of f(int)
int f (int) { ... } // definition of f(int)
int f (cInt) { ... } // error: redefinition of f(int)

-end ]

, .

+5

//testClass.hpp
class testClass {
  public:
     testClass(const int x);
};

//testClass.cpp
testClass::testClass(int x) {}

. pass-by-value. :

void f(int x) { }
void f(const int x) { } // Can't compile both of these.

int main()
{
   f(7); // Which gets called?
}

:

, const / . , const volatile , . [:

typedef const int cInt;
int f (int);
int f (const int); // redeclaration of f(int)
int f (int) { ... } // definition of f(int)
int f (cInt) { ... } // error: redefinition of f(int)

-end example] const ; const , .112) , T, " T", " const T" " " , " T," " const T" msgstr " volatile T."

+4
source

Is it const int * const xnot the same as const int * xsince u already set const?

0
source

All Articles