OK I know a constructor can't have a return value but is it feasible such that I have a class whereby
//in MyClass1.
class MyClass
MyClass1(){}
~MyClass1(){}
MyClass1(int & Result) {Result = InitMyClass()}
int InitMyClass()
//in main.cp
int main(
int result
MyClass theobject(result)
printf("The result of initializing the class is %d", result) 7 6776
Bonj wrote: OK I know a constructor can't have a return value but is it feasible such that I have a class whereby: //in MyClass1.h class MyClass1 { MyClass1(){}; ~MyClass1(){}; MyClass1(int & Result) {Result = InitMyClass()};
int InitMyClass(); }
//in main.cpp
int main() { int result; MyClass theobject(result); printf("The result of initializing the class is %d", result); }
Of course it's feasible, and perfectly legal.
The Standard C++ way of indicating failure in a constructor is to throw an
exception, but there might be reasons sometimes that you don't want to do
that (the only one that comes to mind is a severe performance constraint -
exception throwing is expensive).
-cd
There is another reason not to throw out of a constructor: you don't know
how well or badly the object is constructed, so you can't clean up. This is
most important if the constructor allocates resources (e.g. memory)
dynamically.
e.g.
struct C
{
bool *b1, b2;
C() { b1 = b(); b2 = b();}
bool *b() { if (rand() < 0.5) throw true; else return new b; }
};
int main()
{
C* c;
try
{
c = new C();
}
catch (...)
{
// now, what might c point to? What might c->b1 or c->b2 point to?
}
return;
}
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl... Bonj wrote: OK I know a constructor can't have a return value but is it feasible such that I have a class whereby: //in MyClass1.h class MyClass1 { MyClass1(){}; ~MyClass1(){}; MyClass1(int & Result) {Result = InitMyClass()};
int InitMyClass(); }
//in main.cpp
int main() { int result; MyClass theobject(result); printf("The result of initializing the class is %d", result); }
Of course it's feasible, and perfectly legal.
The Standard C++ way of indicating failure in a constructor is to throw an exception, but there might be reasons sometimes that you don't want to do that (the only one that comes to mind is a severe performance constraint - exception throwing is expensive).
-cd
This situation should be handled by following the RAII principle: Resource
Acquisition Is Initialization.
struct C
{
boost::scoped_ptr<bool> b1, b2;
C() : b1(b()), b2(b()) {}
bool *b() { if (rand() < 0.5) throw true; else return new b; }
};
Now if an exception is thrown you're guaranteed that if b1 was initialized,
it'll be destructed.
-cd
Simon Trew wrote: There is another reason not to throw out of a constructor: you don't know how well or badly the object is constructed, so you can't clean up. This is most important if the constructor allocates resources (e.g. memory) dynamically.
e.g.
struct C { bool *b1, b2; C() { b1 = b(); b2 = b();} bool *b() { if (rand() < 0.5) throw true; else return new b; } };
int main() { C* c; try { c = new C(); } catch (...) { // now, what might c point to? What might c->b1 or c->b2 point to? } return; }
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam > wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl... Bonj wrote: OK I know a constructor can't have a return value but is it feasible such that I have a class whereby: //in MyClass1.h class MyClass1 { MyClass1(){}; ~MyClass1(){}; MyClass1(int & Result) {Result = InitMyClass()};
int InitMyClass(); }
//in main.cpp
int main() { int result; MyClass theobject(result); printf("The result of initializing the class is %d", result); }
Of course it's feasible, and perfectly legal.
The Standard C++ way of indicating failure in a constructor is to throw an exception, but there might be reasons sometimes that you don't want to do that (the only one that comes to mind is a severe performance constraint - exception throwing is expensive).
-cd
Absolutely, but (unless I am thinking fuzzily as usual) that only moves it
down a peg: we can only construct members that themselves obey the RAII
principle. (If boost::scoped_ptr's constructor threw after allocating some
resource then we'd be back to square one.)
S.
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:Oz***************@TK2MSFTNGP12.phx.gbl... This situation should be handled by following the RAII principle:
Resource Acquisition Is Initialization.
struct C { boost::scoped_ptr<bool> b1, b2; C() : b1(b()), b2(b()) {} bool *b() { if (rand() < 0.5) throw true; else return new b; } };
Now if an exception is thrown you're guaranteed that if b1 was
initialized, it'll be destructed.
-cd
Simon Trew wrote: There is another reason not to throw out of a constructor: you don't know how well or badly the object is constructed, so you can't clean up. This is most important if the constructor allocates resources (e.g. memory) dynamically.
e.g.
struct C { bool *b1, b2; C() { b1 = b(); b2 = b();} bool *b() { if (rand() < 0.5) throw true; else return new b; } };
int main() { C* c; try { c = new C(); } catch (...) { // now, what might c point to? What might c->b1 or c->b2 point to? } return; }
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam > wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl... Bonj wrote: OK I know a constructor can't have a return value but is it feasible such that I have a class whereby: //in MyClass1.h class MyClass1 { MyClass1(){}; ~MyClass1(){}; MyClass1(int & Result) {Result = InitMyClass()};
int InitMyClass(); }
//in main.cpp
int main() { int result; MyClass theobject(result); printf("The result of initializing the class is %d", result); }
Of course it's feasible, and perfectly legal.
The Standard C++ way of indicating failure in a constructor is to throw an exception, but there might be reasons sometimes that you don't want to do that (the only one that comes to mind is a severe performance constraint - exception throwing is expensive).
-cd
Simon Trew wrote: Absolutely, but (unless I am thinking fuzzily as usual) that only moves it down a peg: we can only construct members that themselves obey the RAII principle. (If boost::scoped_ptr's constructor threw after allocating some resource then we'd be back to square one.)
Sure. But properly designed RAII containers (like boost::scoped_ptr) will
behave correctly. In the example given, the only thing that can throw is
the new expression, and if that throws boost::scoped_ptr's constructor will
never be entered. If new doesn't throw and scoped_ptr's constructor is
entered, it must either properly recover from any exception or (in reality)
promise not to throw.
-cd
Simon Trew wrote: Absolutely, but (unless I am thinking fuzzily as usual) that only moves it down a peg: we can only construct members that themselves obey the RAII principle.
Well, yes, you gotta start somewhere. :) But once you have a set of nice,
exception-safe, low-level classes such as std::string, std::vector, various
smart pointers, etc, you can build your own classes in terms of them and not
worry about them leaking.
(If boost::scoped_ptr's constructor threw after allocating some resource then we'd be back to square one.)
That's a problem only if you have a mix of exception-safe and unsafe
members. I can't remember the last time I used a raw pointer as a class
member, and that pointer owned the data it pointed to.
--
Doug Harrison
Microsoft MVP - Visual C++
Yep, I was not suggesting that boost::scoped_ptr behaved anything other than
properly.
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... Sure. But properly designed RAII containers (like boost::scoped_ptr) will behave correctly. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jakob Bieling |
last post by:
Hi!
I am using VC++ 7.1 and have a question about return value optimization.
Consider the following code:
#include <list>
#include <string>
struct test
{
|
by: Michael Klatt |
last post by:
class Foo
{
public :
explicit Foo(int i) : m_int(i) {}
private :
int m_int;
};
Foo f(int i)
|
by: Nils O. Selåsdal |
last post by:
given a prototye:
string foo();
What would be the diffrence in calling it, and assigning
the return value in
string a = foo();
vs
string &a = foo();
|
by: gsyoon |
last post by:
hi, all.
I'm trying to make a "framework" to store the return value of a
function to a global memory.
My first attempt was
1)
void store_to_global( char * type_name ) {
if ( strcmp(...
|
by: tshad |
last post by:
I am trying to set up a class to handle my database accesses.
I can't seem to figure out how to get the return value from my dataReader
from these routines (most of which I got elsewhere). They...
|
by: Neelesh Bodas |
last post by:
Hello all,
Just wanted a small clarification on these two points :
1) The following code compiles well -
int f() try {
throw "xyz";
}
catch(...)
{
|
by: lovecreatesbeauty |
last post by:
Hello experts,
Is the following code snippet legal? If it is, how can exit() do the
keyword return a favor and give a return value to the main function?
Can a function call (or only this...
|
by: SzH |
last post by:
The code below demonstrates that the copy constructor of moo is not
called on the first line of main. In spite of this, g++ (version 4.1.2)
refuses to compile it if I make the copy constructor...
|
by: Rahul |
last post by:
While reading "Efficient C++" by Dov Bulka I came across the follown
statement
"In addition, you must also define a copy constructor to "turn on" the
Return Value Optimization(RVO). If the
class...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |