473,408 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Default Pamameter

Now... I have this piece of code: the function DoSomething has a parameter,
but it can be called without it... If it's called without it, it will build
an object of the right type and use it. No use of new/delete! (I need it to
be FAST). No unnecessary building of the object CMyOb when it's not needed
(perhaps it has a very very slow constructor).. I know the compiler will
probably inline DoSomething and it won't exist in the compiled program.

inline void DoSomething(CMyOb *pmyob = NULL)
{
if (pmyob)
_DoSomething(pmyob);
else {
CMyOb myob;
_DoSomething(&myob);
}

void _DoSomething(CMyOb *pmyob)
{
}

The question... Can I do something like (I want better readability of the
source):
inline void DoSomething(CMyOb *pmyob = &CyOb())
{
}

what is the scope of the created CMyOb? Is it created on the stack?

And now the 2nd question... If it's possible to do it, how can I do this:
inline void DoSomething(CMyOb *pmyob = NULL)
{
if (pmyob)
_DoSomething(pmyob);
else {
CMyOb myob;
FillObject(&myob);
_DoSomething(&myob);
}

(the object need to be filled with special data that the constructor of the
object can't know. I can build the FillObject function however I want)

--- thanks
Jul 22 '05 #1
2 1079
Massimiliano Alberti wrote:

The question... Can I do something like (I want better readability of the
source):
inline void DoSomething(CMyOb *pmyob = &CyOb())
{
}
what is the scope of the created CMyOb? Is it created on the stack?

Such a construction is invalid. You cannot take address of a temporary.
However the idea is realizable just use references instead:

inline void DoSomething2(const CMyOb& pmyob = CMyOb())

This is perfectly legal and the lifetime of the temporary object will be
as long as that of the reference to which it is assigned. Of course
temporaries are created on the stack. However you cannot modify a
temporary object so if your code must do it this solution is not
applicable to your problem.
And now the 2nd question... If it's possible to do it, how can I do this:
inline void DoSomething(CMyOb *pmyob = NULL)
{
if (pmyob)
_DoSomething(pmyob);
else {
CMyOb myob;
FillObject(&myob);
_DoSomething(&myob);
}

(the object need to be filled with special data that the constructor of the
object can't know. I can build the FillObject function however I want)


Maybe there is a typo (missing 'not') however I do not see how you could
combine both solutions. You may consider adding a flag to CMyOb
indicating if it was filled, or create a factory function encapsulating
creating and filling of the object.
Regards,
Janusz

Jul 22 '05 #2
Massimiliano Alberti wrote in
news:c1**********@grillo.cs.interbusiness.it:
Now... I have this piece of code: the function DoSomething has a
parameter, but it can be called without it... If it's called without
it, it will build an object of the right type and use it. No use of
new/delete! (I need it to be FAST). No unnecessary building of the
object CMyOb when it's not needed (perhaps it has a very very slow
constructor).. I know the compiler will probably inline DoSomething
and it won't exist in the compiled program.

inline void DoSomething(CMyOb *pmyob = NULL)
{
if (pmyob)
_DoSomething(pmyob);
else {
CMyOb myob;
_DoSomething(&myob);
}

identifiers with a leading underscore followed by an Uppercase letter
are reserved for use by the compile/standard library implemetor, you
shoudn't use them.
void _DoSomething(CMyOb *pmyob)
{
}

The question... Can I do something like (I want better readability of
the source):
inline void DoSomething(CMyOb *pmyob = &CyOb())
{
}

There is really no need to do this (and no you cant do it (*)),
overloading will help:

void DoSomething( CMyOb &ob )
{
// whatever
}

inline void DoSomething()
{
CMyOb ob = FillObject();
DoSomeThing( ob );
}

Note (*) actually you might, but only if you've provided CMyOB with
a member operator & () (a *very* Bad Thing(tm)).
what is the scope of the created CMyOb? Is it created on the stack?

Yes.
And now the 2nd question... If it's possible to do it, how can I do
this: inline void DoSomething(CMyOb *pmyob = NULL)
{
if (pmyob)
_DoSomething(pmyob);
else {
CMyOb myob;
FillObject(&myob);
_DoSomething(&myob);
}

(the object need to be filled with special data that the constructor
of the object can't know. I can build the FillObject function however
I want)


The dodgy _DoSomething name aside there is nothing wrong with the above.
However you might want to change FillObject() to return a CMyOb by value:

CMyOB FillObject()
{
//either:

CMyOb ob;
//whatever
return ob;

//or
//whatever
return CMyOb( /* paramiters calculated by "whatever" */ );
}

With a sutibly compliant compiler calling the above like:

CMyOb ob = FillObject();

Will bypass 1 constructor call, this is called RVO (Return Value
Optimisation). The first form (after //either:) is less widely
implemented than the second, so use the second form if you can.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

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

Similar topics

12
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
0
by: inquirydog | last post by:
Hi- I am using xml to hold configuration data for a project, and using schema to define what the configuration file should look like. I wanted to get some advice on an intelligant way to...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
30
by: Bruce Momjian | last post by:
I have events in the next few weeks in New York City, Copenhagen, Paris, and Atlanta. Check the News section on the web site for more information. I will also be in Amsterdam February 2-3, though...
74
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...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...

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.