A very basic question:

In the standard c++ structure, every .cpp file looks like this:

MyClass::MyClass() ...
MyClass::~MyClass() ...
MyClass::function1(...
MyClass::function2(...
MyClass::function3(...

It looks redundant to reiterate the "MyClass::" every time.
I tried different ways to use namespaces for this, without success.

It there any way to telll the compiler that from this to that point, everything I implement belongs to a certain class?

I'll be glad for any insight into the question of classes and namespaces relations (even the syntax "::" is the same!)

Thank you

Gidi

This is because you're declaring the function as it belongs to that class.

A namespace doesn't replace a class, it contains a number of classes, separating them from the rest of your code.

If you declared like this:

namespace MyApp
{
   class MyClass
   {
      ...Class declaration
   }
}

Then in your main files you would have to call it using MyApp::MyClass::MyStaticMethod(); or MyApp::MyClass myNewClass; (unless you used the "using" statement)

using namespace MyApp;
...
MyClass myNewClass;
...

EDIT: So to re-iterate... MyClass::MyMethod(int myParam) says "The MyClass class has a method called MyMethod that takes an int called myParam and this is the code for it". namespace MyApp{ ... } says "This code has been separated into the "MyApp" section. So to get to it you must go through "MyApp" first"

> It there any way to telll the compiler that from this to that point, everything I implement belongs to a certain class?
There is no way the compiler knows that you are implementing a member outside of a class definition unless the name is qualified. The only way to avoid qualifying definition names is to embed them in the class definition.

class Foo {
public:
    // No qualification needed
    Foo() { cout << "Foo()\n"; }

    // Qualification will be needed
    Foo(int);
};

// See? Qualification was needed
Foo::Foo(int) { cout << "Foo(int)\n"; }

But beware embedding method definitions in the class. It makes them inline, and inline methods can be tricky.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.