Is const parameter in definition, but not really C ++ declaration?

This is similar to (but different from) this question .

Here are some simple test codes to illustrate some of the oddities that I discovered with Sun CC:

//---------------main.cpp
#include "wtc.hpp"

int main(int, char**)
{
  testy t;
  t.lame(99);
  return 0;
}
//--------------wtc.hpp
#ifndef WTC_HPP_INCLUDED
#define WTC_HPP_INCLUDED

class testy
{
public:
  void lame(int );
};

#endif 

//---------------wtc.cpp
#include <iostream>
#include "wtc.hpp"

void testy::lame(const int a)
{
  std::cout << "I was passed " << a << "\n";
}

//---------------makefile
#CXX=CC
CXX =g++
#CXXFLAGS= -g 
CXXFLAGS= -g3 -Wall -Werror

OBJECTS=$(patsubst %.cpp,%.o,$(wildcard *.cpp))

all : $(OBJECTS)
    $(CXX) $(CXXFLAGS) -o $@ $^

.PHONY: clean
clean :
    rm *.o

When it was compiled with g ++, it compiles, links, and does what you expect at startup. You can also add a ++ a; in testy :: lame (), and the compiler will complain about the read-only variable being changed (as it should be).

However, when I compile using CC, I get the following linker error:

CC -g   -c -o main.o main.cpp
CC -g   -c -o wtc.o wtc.cpp
CC -g -o all main.o wtc.o
Undefined                       first referenced
 symbol                             in file
void testy::lame(int)               main.o
ld: fatal: Symbol referencing errors. No output written to all
make: *** [all] Error 1

checking the object code using nm and the C ++ filter, I found that the g ++ version creates testy :: lame (int), while CC creates testy :: lame (const int), therefore, a linker error.

, ( , !); , , Solaris?

+2
7

CC. ++ ( 13.1 ):

, / , . , - , , .

const/volatile, , :

- ; const volatile-, , .

+23

'const' 'const int' . - , , .

+4

, , . , , , . , , ,

question.

+1

const , . , .

0

const void func1(const int) , int --- int, , . , .

, , void func2(const char*) const . , void func3(char* const). , ; , , () 'const'

0

. ++ Alexandrescu # 15. . . IMO const . , . .

int temp = a;

, (const int a) , . , .

0

, const int int.

-2

All Articles