Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default parameters of constructors

Reply
Thread Tools

Default parameters of constructors

 
 
Marcin Vorbrodt
Guest
Posts: n/a
 
      09-07-2003
I know that the default parameter(s) of a constructor can be either
hardcoded values or function calls. But can they be calls to static class
methods as well... example:

Constructor of my CoordinateFrame class:

CoordinateFrame(const Basis &basis = Basis::ORIGIN(), const Point &origin =
Point::ORIGIN());

Thanks,
Martin


 
Reply With Quote
 
 
 
 
Marcin Vorbrodt
Guest
Posts: n/a
 
      09-07-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:KWK6b.2683$(E-Mail Removed) nk.net...
>
> Marcin Vorbrodt <(E-Mail Removed)> wrote in message
> news:bjer80$906$(E-Mail Removed)...
> > I know that the default parameter(s) of a constructor can be either
> > hardcoded values or function calls. But can they be calls to static

class
> > methods as well... example:
> >
> > Constructor of my CoordinateFrame class:
> >
> > CoordinateFrame(const Basis &basis = Basis::ORIGIN(), const Point

&origin
> =
> > Point::ORIGIN());

>
> Are you getting compiler errors or what? If so, what are they,
> and what exact code causes them?
>
> #include <iostream>
>
> class A
> {
> public:
> static int foo() { return 1; }
> };
>
> class B
> {
> public:
> static int foo() { return 2; }
> };
>
> class C
> {
> public:
> int membera;
> int memberb;
>
> C(int parma = A::foo(), int parmb = B::foo())
> : membera(parma), memberb(parmb) {}
> };
>
> int main()
> {
> C c;
> std::cout << c.membera << ", " << c.memberb << '\n';
> return 0;
> }
>
> Output:
>
> 1, 2
>
> -Mike
>
>
>


No, no compiler errors at all. I was just wondering if that was allowed by
the C++ standard, or simply a glitch in my compiler

Martin


 
Reply With Quote
 
 
 
 
Marcin Vorbrodt
Guest
Posts: n/a
 
      09-07-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message
newsbO6b.2841$(E-Mail Removed) ink.net...
>
> Marcin Vorbrodt <(E-Mail Removed)> wrote in message
> news:bjfvuu$pro$(E-Mail Removed)...
> >
> > "Mike Wahler" <(E-Mail Removed)> wrote in message
> > news:KWK6b.2683$(E-Mail Removed) nk.net...
> > >
> > > Marcin Vorbrodt <(E-Mail Removed)> wrote in message
> > > news:bjer80$906$(E-Mail Removed)...
> > > > I know that the default parameter(s) of a constructor can be either
> > > > hardcoded values or function calls. But can they be calls to static

> > class
> > > > methods as well... example:
> > > >
> > > > Constructor of my CoordinateFrame class:
> > > >
> > > > CoordinateFrame(const Basis &basis = Basis::ORIGIN(), const Point

> > &origin
> > > =
> > > > Point::ORIGIN());
> > >
> > > Are you getting compiler errors or what? If so, what are they,
> > > and what exact code causes them?
> > >
> > > #include <iostream>
> > >
> > > class A
> > > {
> > > public:
> > > static int foo() { return 1; }
> > > };
> > >
> > > class B
> > > {
> > > public:
> > > static int foo() { return 2; }
> > > };
> > >
> > > class C
> > > {
> > > public:
> > > int membera;
> > > int memberb;
> > >
> > > C(int parma = A::foo(), int parmb = B::foo())
> > > : membera(parma), memberb(parmb) {}
> > > };
> > >
> > > int main()
> > > {
> > > C c;
> > > std::cout << c.membera << ", " << c.memberb << '\n';
> > > return 0;
> > > }
> > >
> > > Output:
> > >
> > > 1, 2
> > >
> > > -Mike
> > >
> > >
> > >

> >
> > No, no compiler errors at all. I was just wondering if that was allowed

by
> > the C++ standard, or simply a glitch in my compiler

>
> Well, it's good that you check. I'm fairly sure it's valid, I'll
> have to check the standard to be sure. No time right now, but
> I'll post my findings when I return (unless of course someone else
> beats me to it. )
>
> -Mike
>
>
>


Great, cant wait to hear back from you.

Martin


 
Reply With Quote
 
Janusz Szpilewski
Guest
Posts: n/a
 
      09-07-2003
Mike Wahler wrote:

> Well, it's good that you check. I'm fairly sure it's valid, I'll
> have to check the standard to be sure. No time right now, but
> I'll post my findings when I return (unless of course someone else
> beats me to it. )
>


The C++ standard forbids the default parameters being local variables
(8.3.6/7) or the keyword this (8.3.6/. I cannot think of any inherent
reason why constructor might clash with a static member function. Seen
from the run-time environment point of view they are completely unrelated.

Regards,
Janusz

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-08-2003
Marcin Vorbrodt <(E-Mail Removed)> wrote in message
news:bjgc11$7oe$(E-Mail Removed)...

[using the return value of a static member function
as a default function parameter]

> > > No, no compiler errors at all. I was just wondering if that was

allowed
> by
> > > the C++ standard, or simply a glitch in my compiler

> >
> > Well, it's good that you check. I'm fairly sure it's valid, I'll
> > have to check the standard to be sure. No time right now, but
> > I'll post my findings when I return (unless of course someone else
> > beats me to it. )
> >
> > -Mike
> >
> >
> >

>
> Great, cant wait to hear back from you.


What you're doing is valid, according to 8.3.6
Also note that this issue does not only apply
to constructors, but to any function.

-Mike



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments