Can I avoid using the class name in the .cpp file if I declare the namespace in the header?

In C ++, all I want to do is declare a class DisplayInfoin a file .h, and then in the file I .cppdo not need to enter the first DisplayInfo::DisplayInfo()and each function definition.

Unfortunately, I looked at more than 20 topics and my C ++ book for more than two hours and could not solve this problem. I think because I'm trying to use a 10 year Java training in C ++.

First test:

//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 test, I tried to switch the order, so it was

class DisplayInfo
{

  namespace DisplayInfoNamespace
  {
  ...
  }
}

And in the file .cpptry all of the above plus

using namespace DisplayInfo::DisplayInfoNamespace; 

For the third test, I tried forwarding an ad with this heading:

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

VisualStudio2010 express , , .cpp , .

, , 30 , ++: " " ? ? ( , typedefs?)

+5
1

A::A(), .

inplace havinf .

:

// 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;
  }
}

, , , .

0

All Articles