小编ENI*_*GMA的帖子

用户定义的转换函数和转换引用

我遇到以下代码的编译错误:

class SymbolGroup
{
  std::string d_;

public:
  SymbolGroup(std::string a):d_(a){}

  // explicit operator const std::string&() const { return d_;} // compiles
  explicit operator std::string() const { return d_;} // Does not compile
};

inline
bool operator==(const SymbolGroup& lhs, const SymbolGroup& rhs)
{
  return static_cast<const std::string&>(lhs) ==
    static_cast<const std::string&>(rhs);
}

int main(){

  SymbolGroup a("hello");
  SymbolGroup b("hello");

  if (a==b)
    std::cout << "they are the same\n";

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果没有用户定义的类型转换行中的'const'和'&',则不能使用--std = c ++ 11标志在g ++(4.8)中编译:

错误:从类型'const string {aka const std :: basic_string}'表达式的表达式'std :: string&{aka …

c++ type-conversion

6
推荐指数
1
解决办法
964
查看次数

G ++以不同的方式编译.hpp和.cpp文件到共享库

我试图将一个简单的函数编译成共享库对象(.so).该函数位于名为hello.hpp的文件中:

const char* greet(){
  return "hello world";
}
Run Code Online (Sandbox Code Playgroud)

我用:

g++ -shared -fpic hello.hpp -o hello.so
Run Code Online (Sandbox Code Playgroud)

然后它创建一个大小为1.8 MB的hello.so文件.我发现大小不合理,我试图将源文件重命名为hello.cpp而不更改其内容.然后我再次编译:

g++ -shared -fpic hello.cpp -o hello.so
Run Code Online (Sandbox Code Playgroud)

这次,文件只有7 KB.为什么g ++的行为不同只是因为文件名不同?我的平台是Ubuntu 14.04和g ++ 4.8.2.

c++ g++

2
推荐指数
1
解决办法
3861
查看次数

标签 统计

c++ ×2

g++ ×1

type-conversion ×1