如果我这样做:
// In header
class Foo {
void foo(bar*);
};
// In cpp
void Foo::foo(bar* const pBar) {
//Stuff
}
Run Code Online (Sandbox Code Playgroud)
编译器不会抱怨Foo :: foo的签名不匹配.但是如果我有:
void foo(const bar*); //In header
void Foo::foo(bar*) {} //In cpp
Run Code Online (Sandbox Code Playgroud)
代码将无法编译.
到底是怎么回事?我正在使用gcc 4.1.x.
这类似于(但不同于)这个问题.
下面是一些简单的测试代码,用于说明我在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 …
Run Code Online (Sandbox Code Playgroud)