namespaces, copyconstructors, friends
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: namespaces, copyconstructors, friends

  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Angry namespaces, copyconstructors, friends

    1. Why could C++ have not disabled a default copy constructor and why shouldnt it have demanded a
    explicit copy constructor, if such a use case of the class exist, it would have avoided possibility of unintended double destruction.
    2. Why couldnt c++ have prevented operator overloading in global scope or non-class scope? May be it was needed for friends??
    3. Couldnt it have dealt the friend problem by allowing a friend override in the declaration of a class on a argument list or a member declaration??(so wouldnt have required friend global operator overloading, and wouldnt have required c-functions access to private members. ) (but if friendship was not supposed to be taken but given, is there still a solution?)
    4. was namespaces really necessary, when class/struct where sufficient?

    What is the output of the following? What value do such constructs add?
    #include <iostream>
    using namespace std;
    namespace space1
    {
    class A
    {
    int a;
    public:
    void print()
    {
    cout << " space1::A:rint\n";
    }
    };
    void operator << (A& a, int b)
    {
    cout << "I am the space1, int operator in space1\n";
    a.print();
    }
    };


    namespace space2
    {
    class A
    {
    int a;
    public:
    void print()
    {
    cout << " space2::A:rint\n";
    }
    };

    void operator << (A& a, int b)
    {
    cout << "I am the space2::A, int operator in space2\n";
    a.print();
    }

    void operator << (space1::A &a, float b)
    {
    cout << "I am the space1, float in space2 \n";
    a.print();
    }

    };
    using namespace space2;
    void main()
    {

    A a;
    a<<1;
    space1::A b;
    b<<(int)1;
    space2::A c;
    c<<1;

    b << (float)1.0;
    }


    scroll down for the output






















    output:
    I am the space2::A, int operator in space2
    space2::A:rint
    I am the space1, int operator in space1
    space1::A:rint
    I am the space2::A, int operator in space2
    space2::A:rint
    I am the space1, float in space2
    space1::A:rint

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    Re: namespaces, copyconstructors, friends

    Is there some point to this post?

  3. #3
    Join Date
    Aug 2009
    Posts
    4

    Re: namespaces, copyconstructors, friends

    well the point is the specific constructs in c++ questioned may have been designed
    better(or even left out). I want to know if I am wrong, if I am where? This forum is
    for such open discussions about the c++ language right? why such a frustrated
    response???

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: namespaces, copyconstructors, friends

    Quote Originally Posted by vasantharam View Post
    well the point is the specific constructs in c++ questioned may have been designed
    better(or even left out). I want to know if I am wrong, if I am where? This forum is
    for such open discussions about the c++ language right? why such a frustrated
    response???
    So why did you use a "mad" avatar next to the thread title?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,427

    Re: namespaces, copyconstructors, friends

    4. was namespaces really necessary, when class/struct where sufficient?
    I have a class called foo. You have a class called foo and you want to use my foo class also. How are you going to tell the code which foo class to use if you want to use both foo classes?

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: namespaces, copyconstructors, friends

    Quote Originally Posted by vasantharam View Post
    well the point is the specific constructs in c++ questioned may have been designed
    better(or even left out). I want to know if I am wrong, if I am where? This forum is
    for such open discussions about the c++ language right? why such a frustrated
    response???
    C++ is an evolving language. It cannot just discard its heritage every now and then. And it cannot adopt every new fad in an instant. It's the same as with biological evolution; You can't suddenly have 12 fingers just because 12 months in a year is in vouge right now.

    I suggest you pick one C++ "anomality" at a time. Do your homework. Then describe the background and formulate your suggestion.
    Last edited by nuzzle; August 15th, 2009 at 07:08 PM.

  7. #7
    Join Date
    Aug 2009
    Posts
    4

    Red face Re: namespaces, copyconstructors, friends

    Oh the smily was a random pick, didnt realize it means mad-avatar!!!

    If there is a reason to have two foo classes, it probably means, they are
    two different flavors of something.

    Say class elephant is in both my module and your module. In many cases
    both shouldnt exist, if the files are maintained correctly with out overloading
    too many classes into one file, then one possibility is to have only compiled in
    one of the two.

    Other possibility is to ensure the two flavors are brought out by designing it
    as inheritence. i.e white_elephant and black_elephant are derived from elephant,
    so there is no need for same class name any more.

    I have to accept namespaces are a good thing to have (nice-to-have, not-necessary), however,
    global operator overloading/default copy-construction/default constructor/friends???

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: namespaces, copyconstructors, friends

    Quote Originally Posted by vasantharam View Post
    I have to accept namespaces are a good thing to have (nice-to-have, not-necessary),
    You're dead wrong. Namespaces are a necessity. Otherwise you couldn't modularize a program at all.

    I only wish namespaces went even further. For example I would like to be able to declare a class/struct private to a namespace. This would mean that class/struct couldn't be used outside of the namespace. It would be encapsulated within the namespace.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,286

    Re: namespaces, copyconstructors, friends

    Quote Originally Posted by vasantharam
    1. Why could C++ have not disabled a default copy constructor
    You can disable it for a given class that you maintain if you want.

    Quote Originally Posted by vasantharam
    why shouldnt it have demanded a
    explicit copy constructor, if such a use case of the class exist, it would have avoided possibility of unintended double destruction
    Why should it demand an explicit copy constructor when the compiler generated copy constructor will do the right thing?

    Quote Originally Posted by vasantharam
    Say class elephant is in both my module and your module. In many cases
    both shouldnt exist, if the files are maintained correctly with out overloading
    too many classes into one file, then one possibility is to have only compiled in
    one of the two.
    Right. How do you propose to merge my guitar string class with the standard string class, especially when the latter has pretty much nothing to do with guitars?

    Quote Originally Posted by nuzzle
    I only wish namespaces went even further. For example I would like to be able to declare a class/struct private to a namespace. This would mean that class/struct couldn't be used outside of the namespace. It would be encapsulated within the namespace.
    Yeah, you're talking about a language construct equivalent to the detail namespace that is used as a convention by some when the names have to be declared in a header, right?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: namespaces, copyconstructors, friends

    Quote Originally Posted by laserlight View Post
    Yeah, you're talking about a language construct equivalent to the detail namespace that is used as a convention by some when the names have to be declared in a header, right?
    I mean something like this,
    Code:
    namespace N {
       private class N1 {};
       public class N2 {};
       class N3 {};
    }
    In this example N1 would be private to N and could only be used within N. N2 and N3 would work like they do today. I think the ability to declare a class private or public like that would be a very cheap way to massively increase encapsulation in C++ .

    There are suggestions in this direction but these are more far-reaching and thus harder to get accepted, like this for example.

    http://www.open-std.org/jtc1/sc22/wg...2007/n2316.pdf

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,286

    Re: namespaces, copyconstructors, friends

    Quote Originally Posted by nuzzle
    In this example N1 would be private to N and could only be used within N.
    Yeah, I think we're talking about the same thing. The current workaround that I described would be to write it like this:
    Code:
    namespace N {
       class N2 {};
       class N3 {};
    
       namespace detail {
          class N1 {};
       }
    }
    then rely on convention that N::detail::N1 is ignored except by the implementor/maintainer. Of course, if we can get away with it, it would be better to just define N1 in an anonymous namespace in a source file.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center