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

String operations in member initialisation list

Hi,
I'm writing some exception class for a project, and I've got a class called
BasicException that looks like this:
class BasicException
{
private:
std::string Description;
int Number;

public:
BasicException(std::string, int);
friend std::ostream& operator << (std::ostream&, BasicException&);
};
where the string and the int in the constructor initialize the description
and the error number respectively.
I also have a derived class called InvalidCommand:
class InvalidCommand : public Exceptions::BasicException
{
private:
std::string tmp;
public:
InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
std::string(" is invalid."))), BasicException(tmp, 3) {};
};
the string s is the command name, which is then incorporated into the error
description. This compiles fine in Dev-Cpp (which I always thought had
problems handling string types) but it produces a raft of stupid errors in
VC++ (such as template already defined in <algorithm> and other errors of
the sort.
Is it correct to use the string like I'm using it in the constructor of
InvalidCommand? If it is not, can someone think of another way of doing it?
Thanks in advance,
S. Armondi

--
To contact me by email, remove _NOSPAM_ from the address.
--
To contact me by email, remove _NOSPAM_ from the address.
Jul 19 '05 #1
4 3908
Samuele Armondi escribió:
class InvalidCommand : public Exceptions::BasicException
{
private:
std::string tmp;
public:
InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
std::string(" is invalid."))), BasicException(tmp, 3) {};
};


BasicException is constructed before tmp, the order used in the
initializtion list is ignored (some compilers put a warning). And you
probably not need it tmp at all.

Regards.
Jul 19 '05 #2
"Samuele Armondi" <sa****************@hotmail.com> wrote...
Hi,
I'm writing some exception class for a project, and I've got a class called BasicException that looks like this:
class BasicException
{
private:
std::string Description;
int Number;

public:
BasicException(std::string, int);
friend std::ostream& operator << (std::ostream&, BasicException&);
};
where the string and the int in the constructor initialize the description
and the error number respectively.
I also have a derived class called InvalidCommand:
class InvalidCommand : public Exceptions::BasicException
{
private:
std::string tmp;
public:
InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
std::string(" is invalid."))), BasicException(tmp, 3) {};
};
the string s is the command name, which is then incorporated into the error description. This compiles fine in Dev-Cpp (which I always thought had
problems handling string types) but it produces a raft of stupid errors in
VC++ (such as template already defined in <algorithm> and other errors of
the sort.
Is it correct to use the string like I'm using it in the constructor of
InvalidCommand? If it is not, can someone think of another way of doing

it?
For you, the only way to do it right is

InvalidCommand(const std::string& s) :
BasicException(std::string("The command ") + s + " is invalid", 3),
tmp(std::string("The command ") + s + " is invalid")
{
}

The base classes are constructed before members, and member are
constructed in the declaration order. The order in which you
place them in the initialisation list does NOT matter. You relied
on the 'tmp' being constructed before the base class. It is not.

Victor
Jul 19 '05 #3

"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vf************@corp.supernews.com...
"Samuele Armondi" <sa****************@hotmail.com> wrote...
Hi,
I'm writing some exception class for a project, and I've got a class called
BasicException that looks like this:
class BasicException
{
private:
std::string Description;
int Number;

public:
BasicException(std::string, int);
friend std::ostream& operator << (std::ostream&, BasicException&);
};
where the string and the int in the constructor initialize the description and the error number respectively.
I also have a derived class called InvalidCommand:
class InvalidCommand : public Exceptions::BasicException
{
private:
std::string tmp;
public:
InvalidCommand(std::string s) : tmp((std::string("The command ") + s +
std::string(" is invalid."))), BasicException(tmp, 3) {};
};
the string s is the command name, which is then incorporated into the

error
description. This compiles fine in Dev-Cpp (which I always thought had
problems handling string types) but it produces a raft of stupid errors in VC++ (such as template already defined in <algorithm> and other errors of the sort.
Is it correct to use the string like I'm using it in the constructor of
InvalidCommand? If it is not, can someone think of another way of doing

it?
For you, the only way to do it right is

InvalidCommand(const std::string& s) :
BasicException(std::string("The command ") + s + " is invalid",

3), tmp(std::string("The command ") + s + " is invalid")
{
}

The base classes are constructed before members, and member are
constructed in the declaration order. The order in which you
place them in the initialisation list does NOT matter. You relied
on the 'tmp' being constructed before the base class. It is not.

Victor

Thanks for the replies. After reading the post by john Harrison (calling
member funcs from an initialiser list) i tried to following, which compiles
in VC++:
class InvalidCommand : public Exceptions::BasicException
{
private:
std::string& MakeString(std::string& s)
{
s.insert(0, "The command ");
s.append(" is invalid");
return s;
}
public:
InvalidCommand(std::string s) : BasicException(MakeString(s), 3) {};
};
I gather that since MakeString is not accessing any unitialised class
members, the code is ok. Am I correct?
Thanks in advance,
S. Armondi
Jul 19 '05 #4
"Samuele Armondi" <sa****************@hotmail.com> wrote...
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vf************@corp.supernews.com...
"Samuele Armondi" <sa****************@hotmail.com> wrote...
Hi,
I'm writing some exception class for a project, and I've got a class called
BasicException that looks like this:
class BasicException
{
private:
std::string Description;
int Number;

public:
BasicException(std::string, int);
friend std::ostream& operator << (std::ostream&, BasicException&);
};
where the string and the int in the constructor initialize the description and the error number respectively.
I also have a derived class called InvalidCommand:
class InvalidCommand : public Exceptions::BasicException
{
private:
std::string tmp;
public:
InvalidCommand(std::string s) : tmp((std::string("The command ") + s + std::string(" is invalid."))), BasicException(tmp, 3) {};
};
the string s is the command name, which is then incorporated into the

error
description. This compiles fine in Dev-Cpp (which I always thought had
problems handling string types) but it produces a raft of stupid errors in
VC++ (such as template already defined in <algorithm> and other errors of the sort.
Is it correct to use the string like I'm using it in the constructor
of InvalidCommand? If it is not, can someone think of another way of
doing it?
For you, the only way to do it right is

InvalidCommand(const std::string& s) :
BasicException(std::string("The command ") + s + " is invalid",

3),
tmp(std::string("The command ") + s + " is invalid")
{
}

The base classes are constructed before members, and member are
constructed in the declaration order. The order in which you
place them in the initialisation list does NOT matter. You relied
on the 'tmp' being constructed before the base class. It is not.

Victor

Thanks for the replies. After reading the post by john Harrison (calling
member funcs from an initialiser list) i tried to following, which

compiles in VC++:
class InvalidCommand : public Exceptions::BasicException
{
private:
std::string& MakeString(std::string& s)
{
s.insert(0, "The command ");
s.append(" is invalid");
return s;
}
public:
InvalidCommand(std::string s) : BasicException(MakeString(s), 3) {};
};
I gather that since MakeString is not accessing any unitialised class
members, the code is ok. Am I correct?


Yes. And to be on the safe side, make it 'static' (since you don't
need anything from any particular object).

Victor
Jul 19 '05 #5

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

Similar topics

2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
5
by: rossum | last post by:
Either I have found a puzzle, or I am just being stupid. Inside a namespace I have a class with a static const double member. The member is not an int so I cannot initialise it within the class,...
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
4
by: sks | last post by:
hi , i Have a code snippet as follows class ABC { int &r; ABC(int a=0): r(a) {} }; int main() {
11
by: asdf | last post by:
The oder of member initialization is the order in which the members are defined. So the following code is problematic: class X{ int i; int j; public: X(int val):j(val),i(j){}
5
by: Bryan | last post by:
Hi, Where is the proper place to use a member initialization list, the header or cpp file? Does it make any difference? Also, is there any difference between using a member initialization...
2
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
3
by: subramanian100in | last post by:
There are some operations like sort, remove, remove_if which are available as list<Tmember functions; but vector<T>, deque<Tdo not seem have those functions. If am correct in this, what is the...
13
by: Anarki | last post by:
#include <iostream> using namespace std; class ConstantMember { const int m_const; public: ConstantMember(int cons = 10):m_const(cons){} int getConst() const{ return m_const;} };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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.