tags:

views:

144

answers:

2

In the following code, gcc does not instantiate the NSP::Admin and NSP::Server objects. It just skips them.

int main(int argc, char **argv)
{
  // Here we boostrap 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();
}

If I add a parameter to the CTORS they are instantiated. Example :

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

I cannot break point on them with gdb, and stepping skips them. These two objects register themselves with the boost io service and call a method in their CTORS.

NSP is the project namespace.

Using gcc4.2 on FreeBSD, glog, gflags and boost asio.

+3  A: 

Try:

NSP::Admin            admin;
NSP::server           server;

Example program:

#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;
}
Douglas Leeder
+11  A: 

It does not instantiate them because NSP::Admin admin(); does not create any objects.

Instead it is a declaration of a function prototype of a function which returns NSP::Admin object and takes void arguments. It is one of those wierd C++ syntaxes. The second one works because, compiler doesn't get 'confused' thinking that it is a function prototype. It can clearly see that you are creating an object.

To create an object using the default constructor use

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