-
August 15th, 2009, 06:04 PM
#1
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
-
August 15th, 2009, 06:11 PM
#2
Re: namespaces, copyconstructors, friends
Is there some point to this post?
-
August 15th, 2009, 06:45 PM
#3
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???
-
August 15th, 2009, 06:49 PM
#4
Re: namespaces, copyconstructors, friends
Originally Posted by vasantharam
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
-
August 15th, 2009, 06:51 PM
#5
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
-
August 15th, 2009, 07:04 PM
#6
Re: namespaces, copyconstructors, friends
Originally Posted by vasantharam
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.
-
August 15th, 2009, 07:05 PM
#7
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???
-
August 15th, 2009, 07:18 PM
#8
Re: namespaces, copyconstructors, friends
Originally Posted by vasantharam
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.
-
August 16th, 2009, 12:25 AM
#9
Re: namespaces, copyconstructors, friends
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.
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?
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?
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?
-
August 16th, 2009, 05:14 AM
#10
Re: namespaces, copyconstructors, friends
Originally Posted by laserlight
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
-
August 16th, 2009, 09:33 AM
#11
Re: namespaces, copyconstructors, friends
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:etail::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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|