7

In C++, all I want to do is declare a DisplayInfo class in a .h file, and then in the .cpp file, not have to type the first DisplayInfo::DisplayInfo() and every function definition.

Sadly, I've looked at over 20 topics and my C++ book for over two hours now and have not been able to resolve this. I think it's because I'm trying to use my 10-year-old java training in C++.

1st trial:

//DisplayInfo.h  
namespace DisplayInfoNamespace 
{
  Class DisplayInfo 
  {
    public:
    DisplayInfo(); //default constructor
    float getWidth();
    float getHeight();
    ...
  };
}

//DisplayInfo.cpp
using namespace DisplayInfoNamespace;  //doesn't work
using namespace DisplayInfoNamespace::DisplayInfo //doesn't work either
using DisplayInfoNamespace::DisplayInfo //doesn't work
{
  DisplayInfo::DisplayInfo() {}; //works when I remove the namespace, but the first DisplayInfo:: is what I don't want to type 
  DisplayInfo::getWidth() {return DisplayInfo::width;}  //more DisplayInfo:: that I don't want to type
  ...
}

For the second trial, I tried switching the order, so it was

class DisplayInfo
{

  namespace DisplayInfoNamespace
  {
  ...
  }
}

And in the .cpp file, tried all of the above plus

using namespace DisplayInfo::DisplayInfoNamespace; 

For the third trial I tried forward declaring it with this header:

namespace DisplayInfoNamespace
{
  class DisplayInfo;
}
class DisplayInfo
{
public:
...all my methods and constructors...
};

I'm using VisualStudio2010 express and despite carefully reading the error messages have not been able to find the right arrangement of classes and namespaces in the header and .cpp file to make this work out.

And now after I spent 30 minutes typing this, is C++: "Class namespaces"? the answer? (aka no, you have to use typedefs?)

Community
  • 1
  • 1

1 Answers1

0

There is no way to shorten the A::A() definition syntax, when you do it outside of the class.

within the class it would alow you to define the functions inplace without havinf to select the correct scope.

example:

// in *.h
namespace meh {
  class A {
  public:
    A() {
      std::cout << "A construct" << std::endl;
    }

    void foo();
    void bar();
  }

  void foo();
}

void foo();


// in *.cpp

void foo() {
  std::cout << "foo from outside the namespace" << std::endl;
}

void meh::foo() {
  std::cout << "foo from inside the namespace, but not inside the class" << std::endl;
}

void meh::A::foo() {
  std::cout << "foo" << std::endl;
}


namespace meh {
  void A::bar() {
    std::cout << "bar" << std::endl;
  }
}

As you can see, namespaces would rather add another thing to put in front of your method name, rather than remove one.

scones
  • 3,317
  • 23
  • 34
  • Thanks, but that seems so odd that there's no way to bypass that. Am I meant to define the class in the .cpp file or something? – user2247872 Apr 05 '13 at 07:49
  • no, but if you do not, you have to specify where foo() comes from (in the example). i'll edit to clear that up. – scones Apr 05 '13 at 07:51
  • If you are willing to dump the source file and stick to the header (which is the case when defining a template class/function) you can put your implementation in the header and thus not have to add the class' scope in front of the declaration of the functions since these are also definitions. – rbaleksandar Aug 25 '16 at 06:46
  • @rbaleksandar True, but defining everything in the header file leads to the arguments about header only vs header files and all the PROs and CONs of that and all the exceptions. – scones Aug 25 '16 at 10:40