namespace within class

C

carlosp

Hello, I am trying to organize a big project. At this point I have a
class with ~100 methods. I would like to organize them in a similar
way as with namespaces, but the declaration

namespace myNamespace { }

within the class gives an error

g++ -DNOWARNDRMT -Wall -Wextra -o 2dmera 2dmera.cpp -litpp
../qubit_gate.cpp:476: error: expected unqualified-id before
'namespace'
make: *** [2dmera] Error 1
nike:mera2d carlosp$


What would be the best way to organize my big class??? (I am still
sort of rookie in c++)

Thanks!
 
D

DerTopper

Hello, I am trying to organize a big project. At this point I have a
class with ~100 methods. I would like to organize them in a similar
way as with namespaces, but the declaration

namespace myNamespace { }

within the class gives an error

That's right, namespaces inside classes are not allowed, and when you
think about, this wouldn't make much sense. What would like to achieve
with namespaces inside classes?
What would be the best way to organize my big class??? (I am still
sort of rookie in c++)

This depends. Having a class with 100 methods doesn't have to be bad
design (the CWnd class of MS's MFC has about this number of methods).
In general, however, a class with too many methods is a strong
indicator of bad design. In such a case you should try to separate the
class into several classes, each of them with a smaller coherent
interface. If your source code is not confidential, you could post a
small snapshot of the class so that we can help you better.

Regards,
Stuart
 
C

carlosp

That's right, namespaces inside classes are not allowed, and when you
think about, this wouldn't make much sense. What would like to achieve
with namespaces inside classes?


This depends. Having a class with 100 methods doesn't have to be bad
design (the CWnd class of MS's MFC has about this number of methods).
In general, however, a class with too many methods is a strong
indicator of bad design. In such a case you should try to separate the
class into several classes, each of them with a smaller coherent
interface. If your source code is not confidential, you could post a
small snapshot of the class so that we can help you better.

Regards,
Stuart

Thank you very much. well actually it has a big number of small
routines that move sutff and so on. I am not really willing to create
new clases, that seems to be to much of an effort.

So in order to have it available everywhere I have it in a svn server.
It cannot be compiled as it is downloaded as it requires some other
files that are not there. Still you can take a look. The file I want
to organize is "qubit_gate.cpp". I am also looking forward to any
suggestions regarging programing style!

Download with
svn co https://mera2d.svn.sourceforge.net/svnroot/mera2d



Thanks,
Carlos
 
D

Daniel Pitts

carlosp said:
Thank you very much. well actually it has a big number of small
routines that move sutff and so on. I am not really willing to create
new clases, that seems to be to much of an effort.

Is the class concerned with exactly one thing? If not, then you realy
should consider separating your concerns, possibly moving members into
new classes which stand-alone, and using composition.

How much effort is it to create new classes, really? Adding namespaces
won't actually improve your design, but splitting the class into several
classes -- each with their own core concern -- likely will improve it
significantly.

Also, think about this: Code is read and modified a lot more often than
it is written. If you don't spend a little extra time/effort to make it
clean now, you'll spend a lot of time and effort to deal with it later.
 
C

carlosp

Is the class concerned with exactly one thing? If not, then you realy
should consider separating your concerns, possibly moving members into
new classes which stand-alone, and using composition.

How much effort is it to create new classes, really?  Adding namespaces
won't actually improve your design, but splitting the class into several
classes -- each with their own core concern -- likely will improve it
significantly.

Also, think about this: Code is read and modified a lot more often than
it is written.  If you don't spend a little extra time/effort to make it
clean now, you'll spend a lot of time and effort to deal with it later.

Thanks a lot! I will consider your suggestion, though its not clear
for me at this point how to proceed, regarding design. I guess I'll
have to think a bit...

Thanks for your help!
 
Joined
Jun 4, 2010
Messages
1
Reaction score
0
Multiple inheritance

Hi,
Maybe the following would work:

class A {
// data / functions related to A
};

class B {
// data / functions related to B
};

class C : public A, public B {
// data /functions that operate on both
};

C c_instance;
c_instance.A::function( ); // calls the function in A.

If the function name is unique, then you can just do
c_instance.function();

This seems to basically do what you want.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top