Where to place definations and declerations ?

V

vivekian

Created a class , which is declared within a namespace .

namespace mySpace {
class X {
/* members */
}

This class task is to be used by another class Y , 'using namespace
mySpace' directive.

I have currently placed the above code containing class X in a header
file x.h and included in y.c. But not sure as to where the
declarations and definations should be placed ? Do i place the
declarations in x.h and definations in x.c , and then just include x.h
.. Not sure about the big picture.

thanks in advance,
vivekian
 
S

Sunil Varma

You can write the definitions of the class X in the x.h file itself.

Any way it's a good practice to have the declerations in the .h file
and the definitions in the .cpp/.c file.

Ex.:
------------------------------------------------------------------------------------
x.h
******************
namespace mySpace {
class X {
/* declerations of members */
};
}
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
y.cpp
***************************
namespace mySpace{
class X {
/* definitions of members */
};
using namespace mySpace;
class y{
/* Use the objects of class X*/
};
 
D

davidrubin

vivekian said:
Created a class , which is declared within a namespace .

namespace mySpace {
class X {
/* members */
}

This class task is to be used by another class Y , 'using namespace
mySpace' directive.

I have currently placed the above code containing class X in a header
file x.h and included in y.c. But not sure as to where the
declarations and definations should be placed ? Do i place the
declarations in x.h and definations in x.c , and then just include x.h
. Not sure about the big picture.

Yes, declare the public interface in x.h, and provide the
implementation in x.c(pp). Then,

a. forward-declare X in y.h and include x.h in y.c if X is used in the
interface of Y, or
b. include x.h in y.h if X is used in the implementation of Y

In either case, I recommend scoping X with mySpace (i.e., specifying
mySpace::X rather than 'using namespace mySpace') unless Y is also in
the mySpace namespace. This way you avoid various symbol collision and
name-lookup problems that may arise. /david
 
E

Ed

in x.h
namespace mySpace {
class X {
public:
void funcX(void);
};
}

in x.cpp
#include "x.h"
namespace mySpace {
void X::funcX(void) {
// do something here.
}
}

in y.h
class Y {
public:
void funcY(void);
};

in y.cpp
#include "y.h"
#include "x.h"
void Y::funcY(void) {
mySpace::X x;
x.funcX();
}

Hope this helps. Ed
 
P

Peter_Julian

| Created a class , which is declared within a namespace .
|
| namespace mySpace {
| class X {
| /* members */
| }
|
| This class task is to be used by another class Y , 'using namespace
| mySpace' directive.
|
| I have currently placed the above code containing class X in a header
| file x.h and included in y.c. But not sure as to where the
| declarations and definations should be placed ? Do i place the
| declarations in x.h and definations in x.c , and then just include x.h
| . Not sure about the big picture.
|
| thanks in advance,
| vivekian
|

You don't have to name the files MySpace_X.* but i'ld recommend it in
the case you plan in the future to declare another class X in another
namespace.

// myspace_x.h
#ifndef MYSPACE_X_H_
#define MYSPACE_X_H_ // include guard

namespace MySpace {

class X
{
int m_x;
public:
X();
X(int x);
~X();
/* Member Functions */
int getn() const;
};

} // namespace

#endif // include guard MYSPACE_X_H_
___
// myspace_x.cpp
#include "myspace_x.h"
using MySpace::X; // using directive

X::X() : m_x(0)
{
}

X::X(int n) : m_x(n)
{
}

X::~X()
{
}

int X::getn() const
{
return m_x;
}
___

// test.cpp
#include "myspace_x.h"
#include <iostream>
#include <ostream>

int main()
{
MySpace::X x;
MySpace::X y(99);

std::cout << "x = " << x.getn() << std::endl;
std::cout << "y = " << y.getn() << std::endl;

return 0;
}

/*
x = 0
y = 99
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top