Click here to Skip to main content
15,886,026 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionDos command Pin
MsmVc3-Feb-11 18:17
MsmVc3-Feb-11 18:17 
QuestionRe: Dos command Pin
Rajesh R Subramanian3-Feb-11 18:39
professionalRajesh R Subramanian3-Feb-11 18:39 
AnswerRe: Dos command Pin
Andrew Brock3-Feb-11 18:49
Andrew Brock3-Feb-11 18:49 
GeneralRe: Dos command Pin
MsmVc4-Feb-11 0:27
MsmVc4-Feb-11 0:27 
AnswerRe: Dos comman Pin
Cool_Dev3-Feb-11 23:03
Cool_Dev3-Feb-11 23:03 
AnswerRe: Dos command Pin
csrss3-Feb-11 23:25
csrss3-Feb-11 23:25 
GeneralRe: Dos command Pin
MsmVc4-Feb-11 1:11
MsmVc4-Feb-11 1:11 
QuestionWould you all call this namespace abuse? Pin
asincero3-Feb-11 14:06
asincero3-Feb-11 14:06 
I've been thinking of adopting a new style of namespace use. Well, new to me because I've never seen it used this way anywhere else. The common thing to do in C++ is to have one class to be declared per header file. The only other things that would be declared in the header would be closely related functions and types (helper functions, friend classes, typedefs, enums, etc.). Often, the header file is named after the class.

I propose declaring each class inside it's own namespace. The namespace would be named after the class, except in all lowercase with words seperated by underscores. Classes would be named using the traditional PascalCasingScheme. Related classes and functions would go inside this namespace. Any public nested classes, typedefs, and enums would be at namespace scope instead of being declared inside the class.

For example, this is the before:

// in fancy_widget.h

class FancyWidget
{
public:
    typedef std::vector<std::string>    WidgetIds;
    typedef std::map<std::string, int>  WidgetPrices;

    enum WidgetColors
    {
        RED,
        WHITE,
        BLUE
    };

    class Exception : public std::exception
    {
        ...
    };

    class InvalidWidgetException : public Exception
    {
        ...
    };

    ...
};


and this is the after:

// in fancy_widget.h

namespace fancy_widget
{
    typedef std::vector<std::string>   WidgetIds;
    typedef std::map<std::string, int> WidgetPrices;    

    enum WidgetColors
    {
        RED,
        WHITE,
        BLUE
    };

    class Exception : public std::exception
    {
        ...
    };

    class InvalidWidgetException : public Exception
    {
        ...
    };

    class FancyWidget
    {
        ...
    };
}


I tend to make things that are closely related to a class nested inside the class declaration, friend classes in particular. However, this practice always led to a convoluted class declaration. Using this style moves it out of the class declaration, but still conveys a strong association with that class through the namespace.

I can't think of any negatives to this approach, hence why I'm posting this to the forum. I don't think the extra typing whenever you needed to use anything inside "fancy_widget" would be a bother and if it did there's always the "using" keyword. Anybody see anything wrong with doing this?

This sort of thing seems to be somewhat common in Python (except they frown upon using underscores in package names). Which is why I've started thinking about carrying it over into my C++.
AnswerRe: Would you all call this namespace abuse? Pin
Madhu Nair3-Feb-11 16:33
Madhu Nair3-Feb-11 16:33 
GeneralRe: Would you all call this namespace abuse? Pin
asincero3-Feb-11 16:45
asincero3-Feb-11 16:45 
GeneralRe: Would you all call this namespace abuse? Pin
Stefan_Lang3-Feb-11 21:55
Stefan_Lang3-Feb-11 21:55 
AnswerRe: Would you all call this namespace abuse? Pin
Alain Rist3-Feb-11 20:31
Alain Rist3-Feb-11 20:31 
QuestionRe: Would you all call this namespace abuse? Pin
bob169724-Feb-11 5:03
bob169724-Feb-11 5:03 
QuestionChange Image size Pin
john56322-Feb-11 20:32
john56322-Feb-11 20:32 
AnswerRe: Change Image size Pin
Madhu Nair2-Feb-11 21:17
Madhu Nair2-Feb-11 21:17 
AnswerRe: Change Image size Pin
S p k 5213-Feb-11 1:00
S p k 5213-Feb-11 1:00 
AnswerRe: Change Image size Pin
Cool_Dev3-Feb-11 2:01
Cool_Dev3-Feb-11 2:01 
QuestionError in Release() throw function Pin
Anu_Bala2-Feb-11 19:30
Anu_Bala2-Feb-11 19:30 
AnswerRe: Error in Release() throw function Pin
Andrew Brock2-Feb-11 19:43
Andrew Brock2-Feb-11 19:43 
GeneralRe: Error in Release() throw function Pin
Anu_Bala2-Feb-11 20:05
Anu_Bala2-Feb-11 20:05 
GeneralRe: Error in Release() throw function Pin
Andrew Brock2-Feb-11 20:35
Andrew Brock2-Feb-11 20:35 
GeneralRe: Error in Release() throw function Pin
Richard MacCutchan2-Feb-11 21:54
mveRichard MacCutchan2-Feb-11 21:54 
QuestionGetting the error Link2001: unresolved reference to the function Pin
charanmanjunath2-Feb-11 18:14
charanmanjunath2-Feb-11 18:14 
AnswerRe: Getting the error Link2001: unresolved reference to the function Pin
_AnsHUMAN_ 2-Feb-11 18:20
_AnsHUMAN_ 2-Feb-11 18:20 
GeneralRe: Getting the error Link2001: unresolved reference to the function Pin
charanmanjunath2-Feb-11 18:25
charanmanjunath2-Feb-11 18:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.