1
namespace t {
    class A {};
}

How can create an object of class A?

EDIT :

namespace beta {
  class TESSDLL_API TessBaseAPI 
  { 
     public: 
           TessBaseAPI(); 
           virtual ~TessBaseAPI(); 
  }
}

This is the class defined inside beta  namespace . Now how do i call the constructor? is

tess = new beta::TessBaseAPI(); 

correct?

5 Answers 5

5

As you would normally do. The only difference is that A is inside the namespace t. So you can :

use the scope resolution operator every time you want to use A :

t::A a;

use the using directive

using namespace t; 
A a;

or, as Luc Danton pointed out use the using declaration

using t::A;
A a;

following your edit :

Assuming that your class declaration is ending with a ; as in

namespace beta {
  class TESSDLL_API TessBaseAPI 
  { 
     public: 
           TessBaseAPI(); 
           virtual ~TessBaseAPI(); 
  };
}

Then the correct way to call the constructor is :

beta::TessBaseAPI * tess = beta::TessBaseAPI();

or

beta::TessBaseAPI tess;
Sign up to request clarification or add additional context in comments.

1 Comment

using t::A; is called a using declaration while using namespace t; is a using directive.
3

Either

namespace t
{
    A a;
}

or

t::A a;

In the first case a lives inside namespace t (in fact its full name is ::t::a). In the second case a is global.

(note: your class A{} is missing a ; after })

Comments

2
namespace t
{
    class A {} a;
    A another_a;
}

Now use t::a or t::another_a.

If you want to make another object:

t::A another_a_out_of_namespace_scope;

Comments

0

Use a semicolon after the declaration of the class, and use:

t::A my_object;

muntoo's answer allows you to create a single instance of the class and is also correct.

1 Comment

class TESSDLL_API TessBaseAPI { public: TessBaseAPI(); virtual ~TessBaseAPI(); } this is the class defined inside beta namespace . Now how do i call the constructor? is tess = new beta::TessBaseAPI(); correct?
0

For an object named x;

t::A x;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.