473,407 Members | 2,314 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,407 software developers and data experts.

Best way to exit a C++ Class Constructor

MrPickle
100 100+
The title says it all really?

What's the best way to exit a class constructor?
Jun 28 '09 #1
6 8719
Meetee
931 Expert Mod 512MB
Can you explain your question a bit more? When any object is created constructor is called...! What is inside your constructor class? If you can add your code snippet here!
Jun 29 '09 #2
MrPickle
100 100+
Well in the constructor, say there's an error and I need to exit the constructor, normally you would use return, but because the constructor has return type I cannot use return.

eg:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. class MyClass
  4. {
  5.    public:
  6.       MyClass(int i);
  7.       int num;
  8. }
  9.  
  10. MyClass::MyClass(int i)
  11. {
  12.    if(i < 10)
  13.    {
  14.       std::cout << "Error! i is less than 10\n";
  15.       //Exit the constructor how?
  16.    }
  17.  
  18.    num = i;
  19. }
Just a quick example.
Jun 29 '09 #3
JosAH
11,448 Expert 8TB
Of course, in your example you can silently set i to 10 and use that value but if there really is no good default value to use, throw an exception. The object won't be constructed then (a handy feature, write that down!) and it is up to the caller to fix it, i.e. either catch the exception and deal with it or die.

kind regards,

Jos
Jun 29 '09 #4
MrPickle
100 100+
I see lots of different things when I look around at throw.

I see things like:
throw 1;
throw std::runtime_error("Message");

What should I be doing?
Jul 1 '09 #5
JosAH
11,448 Expert 8TB
@MrPickle
Throw a specific exception; the runtime_error exception has several subclasses that may be more applicable. It also has a sibbling class: 'logic_error'; it's up to you.

kind regards,

Jos
Jul 1 '09 #6
MrPickle
100 100+
@JosAH
Thank you, I will look into that.
Jul 1 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
4
by: Robert Zurer | last post by:
Hello All, Is it considered a best practice to always write a parameterless constructor for any object - just in case? I'm not sure. I want my object to have all it absolutely requires to...
5
by: Doug Handler | last post by:
In C# 2.0, w/ the Program.cs file, i noticed that the Application.Run(new appname()) is run twice. I have in my constructor to do a check for a settings file, and if it isn't found, the...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
9
by: Gerard Kramer | last post by:
Presently I have some difficulties with the following type of code which is understandably causing a segfault: //---- begin code ---- #include <valarray> #include <iostream> using...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
14
by: raylopez99 | last post by:
Question: the "myList" below is the same (works the same) whether instantiated inside the (normal) constructor, as below, and as my standard practice, or outside? RL public class AClass {...
6
by: kamsmartx | last post by:
I'm new to programming and need some help figuring out an algorithm. I need to design some kind of algorithm which will help us define capacity for one of our portfolios....here's the problem...
3
by: Rudi | last post by:
Hello, following problem: At program end or release an assembly a serial device should get a final exit sequence. How can I do this? With Dispose() it's no problem, but this assembly is used...
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
0
BarryA
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...
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
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
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...
0
tracyyun
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...
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...

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.