473,569 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implicit constructor declaration (user annoyed)

sb
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n ) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.
Jul 22 '05 #1
9 2650

"sb" <sp**********@y ahoo.com> wrote in message
news:22******** *************** ***@posting.goo gle.com...
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n ) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.


Personally I would prefer no constructors to be implicitly defined. But I
think the purpose behind the rules as they are is to define the minimum
implicit constructors will remaining compatible with C.

E.g. this code has to compile as C and C++.

// C code
typedef struct
{
int x;
} X;

X x; // use of implicit default constructor
X y = x; // use of implicit copy constructor

As soon as you define one constructor, you are no longer compatible with C
anyway, so the rules can be tightened.

john
Jul 22 '05 #2

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:c1******** *****@ID-196037.news.uni-berlin.de...

"sb" <sp**********@y ahoo.com> wrote in message
news:22******** *************** ***@posting.goo gle.com...
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n ) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}


Actually you don't have to do that

my_vec() {}

is good enough, no too many keystrokes. Or even this

my_vec(size_t n = 0) : vector<int>(2*n ) {}

And don't publically inherit from STL container classes, its bad form.

john
Jul 22 '05 #3

"sb" <sp**********@y ahoo.com> wrote in message
news:22******** *************** ***@posting.goo gle.com...
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n ) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.


struct my_vec : public vector<int> {
double foo;

public:
my_vec(std::vec tor<int>::size_ type n = 0) // default ctor
: vector<int>(2*n ) {} };
};

BTW why are you inheriting from std::vector?

-Mike
Jul 22 '05 #4
sb wrote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.
...


Not exactly true. Copy constructor is always implicitly declared (unless
you explicitly declare copy constructor yourself). Explicit declaration
of non-copy constructor will not suppress implicit declaration of copy
constructor.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #5
sb wrote:
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec: public vector<int> { private: double foo; public:
my_vec(size_t n = 0): vector<int>(2*n ) { } // etc.
};
Does anyone else find this behavior annoying?
Not me.
Instead, I would prefer
for the compiler to implicitly declare all constructors
that are not declared by the user.

Jul 22 '05 #6
"sb" <sp**********@y ahoo.com> wrote in message
news:22******** *************** ***@posting.goo gle.com...
If there is at least one user-defined constructor, no constructors are
implicitly declared.

You certainly wrote an unconvincing example:
struct my_vec : public vector<int> {
deriving from std::vector ? ugh
double foo;
public data member. ugh again
my_vec(size_t n) : vector<int>(2*n ) {}
this constructor converts integers to my_vec's because you neglected to
declare it explicit
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
I am tempted to say that you have already done a lot of stupid typing, but
that wouldn't be a quality statement, so I won't. :)
my_vec() : vector<int>() {}
my_vec() {} doesn't look so onerous
// etc.
};

Does anyone else find this behavior annoying? Instead, I would prefer
for the compiler to implicitly declare all constructors that are not
declared by the user.


Perhaps you are a bit too easily annoyed. This rule seems reasonable,
doesn't stop anybody from doing what they want, and in any event is already
established by many years of usage.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #7
sb
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<c1******* ******@ID-196037.news.uni-berlin.de>...
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:c1******** *****@ID-196037.news.uni-berlin.de...

"sb" <sp**********@y ahoo.com> wrote in message
news:22******** *************** ***@posting.goo gle.com...
If there is at least one user-defined constructor, no constructors are
implicitly declared.

struct my_vec : public vector<int> {
double foo;
my_vec(size_t n) : vector<int>(2*n ) {}
// oops, no default ctor any more
// have to do a lot of stupid typing now , like:
my_vec() : vector<int>() {}

Actually you don't have to do that

my_vec() {}

is good enough, no too many keystrokes. Or even this

my_vec(size_t n = 0) : vector<int>(2*n ) {}


The snippet is merely an illustration. It could have been

my_vec(size_t n) : vector<int>(2*n + 1) {}

And don't publically inherit from STL container classes, its bad form.


Arghh! If I had a dime for every time I heard this...

Should I use containment and redefine all methods instead? (lots and
lots of stupid wrapper code). And what if I want my_vec to *BE* a
vector<int> ? No "style" or "possible misuse" considerations will make
up for such braindamage.

I'm sure some people feel very productive when they write

private:
t m_foo;
public:
const t& foo() const { return m_foo; }
t& foo() { return m_foo; }

and then go on to replicate most of the contents of std::vector.
Wheeee! Accessors! Const-correct ones! Wheee! 300 lines of code in one
hour. The manager must be realy proud of me! Thanks. I think I'll
pass.
Jul 22 '05 #8
> > And don't publically inherit from STL container classes, its bad form.

Arghh! If I had a dime for every time I heard this...

Should I use containment and redefine all methods instead? (lots and
lots of stupid wrapper code). And what if I want my_vec to *BE* a
vector<int> ? No "style" or "possible misuse" considerations will make
up for such braindamage.


The only case I heard where someone convincingly argued for deriving from a
standard container was a teacher who wanted a class exactly like std::string
but wanted to catch null pointer errors, for the benefit of his students who
were making that sort of error.

Almost every time I see derivation from a standard container it is because
someone wants to avoid the trouble of writing three of four wrapper methods
and doesn't know or doesn't care that they are needlessly inheriting scores
of methods from the parent class.

So I'll revise my advice, don't inherit from std::vector unless you have
good reason and you know what you are doing. I guess there is a small number
of cases where you want a class with *exactly* the same interface as
std::vector but with some additional behaviour, but I think its very rare.

john
Jul 22 '05 #9
On 23 Feb 2004 22:52:49 -0800, sp**********@ya hoo.com (sb) wrote:

I'm sure some people feel very productive when they write

private:
t m_foo;
public:
const t& foo() const { return m_foo; }
t& foo() { return m_foo; }

and then go on to replicate most of the contents of std::vector.
Wheeee! Accessors! Const-correct ones! Wheee! 300 lines of code in one
hour. The manager must be realy proud of me! Thanks. I think I'll
pass.


Actually those 300 lines of code can save you *lots* of time. If
m_foo is public, what will happen when you will have to change
your class's implementation? Imagine this:

class T_Wrapper {
// no const-correctness in this class because it isn't
// what we're talking about
public:
T foo;
void print() {
string s = makeStringRepre sentation();
cout << s;
}
string makeStringRepre sentation() {
// lots of time-consuming processing here
}
};

And in the client code:

void f(T_Wrapper& tw) {
tw.foo = someValue;
tw.print();
}

One day you decide to optimize makeStringRepre sentation():

class T_Wrapper {
public:
T foo;
string cachedStringRep ;
bool cacheIsUpToDate ;
void print() {
if ( ! cacheIsUpToDate ) {
cachedStringRep = makeStringRepre sentation();
cacheIsUpToDate = true;
}
cout << cachedStringRep ;
}
string makeStringRepre sentation() {
// same as above
}
void setFoo(T newFoo) {
foo = newFoo;
cacheIsUpToDate = false; // let's not forget this
}
};

Well, now f() is broken, because it changes foo without setting
cacheIsUpToDate to false. And so is all your client code. You
have to rewrite (or at least check) *everything*.

I think data members should be private even if they are part of
the interface. Unless, of course, you have some good reason to
do otherwise.

And, by the way, const-correctness is useful too...
;-)

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4542
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
3
43291
by: Jason | last post by:
Hi, Im running windows xp pro and compiling using dev c++ 4. I have the following situation: #include <iostream> #include <string> using namespace std; int main() {
6
5079
by: George | last post by:
Dear C++ Afficionados, I know the compiler will generate a copy assignment operator (amongst other fn's) if you don't provide one in your class declaration. But the parameter types can vary, and the compiler decides between const, reference, and (with xlC) volatile. Is there any way to dump the object file and see what the compiler...
4
8278
by: Annoyed Programmer | last post by:
Maybe this question has been addressed before, but I just stumbled with the issue so I'll post it here. Since I can remember array declaration in any language I've worked(Basic/VB/VB.NET/C/C++) was: (Example in Basic-way) Dim Arr('UpperBound') as Type Ex. Dim Arr(0) as String gives you a String Array with 1 element
25
2037
by: Chad Z. Hower aka Kudzu | last post by:
I have defined some implicit convertors so that I can do: MyStruct x = 4; The implicit convert the 4 into MyStruct. But its essentially a copy constructor and thus if I had: MyStruct x; x.SomethingElse = 5; x = 4;
6
2573
by: Adam Badura | last post by:
Hi! I have a class like: template<int size_integer, int size_fraction> class number { template<typename charT, typename traitsT> explicit number(const charT* textual, int radix = 10);
9
3535
by: Alex Vinokur | last post by:
Compiler Green Hills C++, Version 4.0.6 --- foo.cpp --- struct A { }; struct B { B() {}
74
15911
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
3
2464
by: Jess | last post by:
Hello, I can perform implicit conversion through constructor, like class A{ public: A(int x):a(x){}; int a; };
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.