Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用gcc强制实例化对象_C++_Gcc - Fatal编程技术网

C++ 使用gcc强制实例化对象

C++ 使用gcc强制实例化对象,c++,gcc,C++,Gcc,在以下代码中,gcc不会实例化NSP::Admin和NSP::Server对象。 它只是跳过了它们 int main(int argc, char **argv) { // Here we bootstrap google logging // we also install the signal handler google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); // now

在以下代码中,gcc不会实例化NSP::Admin和NSP::Server对象。 它只是跳过了它们

int main(int argc, char **argv)
{
  // Here we bootstrap google logging
  // we also install the signal handler
  google::InitGoogleLogging(argv[0]);
  google::InstallFailureSignalHandler();
  // now we parse the arguments with gflags
  google::ParseCommandLineFlags(&argc, &argv, true);

  NSP::Admin            admin();
  NSP::server           server();

  DLOG(INFO) << "boost io_service run";
  NSP::IOService::getIOService().run();
}
我不能用gdb在他们身上突破点,而stepping会跳过他们。 这两个对象向boost io服务注册它们自己,并在它们的CTOR中调用一个方法

NSP是项目名称空间

在FreeBSD上使用gcc4.2, glog、gflags和boost asio。

试试:

NSP::Admin            admin;
NSP::server           server;
示例程序:

#include <iostream>

class Foo
{
public:
        Foo() { std::cout << "CTR" << std::endl; }
};

int a()
{
    std::cout << "a in" << std::endl;
    Foo foo();
    std::cout << "a out" << std::endl;
}

int b()
{
    std::cout << "b in" << std::endl;
    Foo foo;
    std::cout << "b out" << std::endl;
}

int main()
{
    a();
    b();
    return 0;
}
#包括
福班
{
公众:

Foo(){std::cout它不会实例化它们,因为
NSP::Admin();
不会创建任何对象

而是一个函数的函数原型的声明,它返回NSP::admin对象并取无效的参数。它是C++的语法之一。第二个是因为编译器不会混淆它是函数原型。它可以清楚地看到你正在创建一个对象。 要使用默认构造函数创建对象,请使用

NSP::Admin            admin;   // (without parenthesis)
NSP::server           server;
NSP::Admin            admin;   // (without parenthesis)
NSP::server           server;