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

derived members & initializer list

Hi,
Why does the member initialization list not work for derived members
but the assignment within the constructor works fine ?
ex ->
class X{
protected :
short int i ;`
};
class Y: public X{
public :
Y() ;
} ;
Y::Y() : i(10) {} // this gives compilation error
Y::Y() { i = 10 ;} // this works fine .

why is that ?
regards,
Aman .
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 19 '05 #1
4 2277
Aman wrote:
Hi,
Why does the member initialization list not work for derived members
but the assignment within the constructor works fine ?
ex ->
class X{
protected :
short int i ;`
};
class Y: public X{
public :
Y() ;
} ;
Y::Y() : i(10) {} // this gives compilation error
Y::Y() { i = 10 ;} // this works fine .

why is that ?


Because those members are initialized by the constructor of the base, you
cannot initialize them again. Only assign.

--
WW aka Attila
Jul 19 '05 #2

"Aman" <am**@techie.com> wrote in message news:26************************************@mygate .mailgate.org...
Y::Y() : i(10) {} // this gives compilation error
You can only initialize members and your direct base classes.
i is neither, it's a member of your base class.
Y::Y() { i = 10 ;} // this works fine .


This is assignment. i is an accessible base class member and hence
the access is fine.
Jul 19 '05 #3
gotcha .
Thanks guys.


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 19 '05 #4
<Aman>
Hi,
Why does the member initialization list not work for derived members
but the assignment within the constructor works fine ?
ex ->
class X{
protected :
short int i ;`
};
class Y: public X{
public :
Y() ;
} ;
Y::Y() : i(10) {} // this gives compilation error
Y::Y() { i = 10 ;} // this works fine .

why is that ?

</>
class X{
public :
X(int a):i(a){}
protected :
int i;
};
class Y: public X{
public :
Y() ;
} ;
Y::Y() : X(10) {} // fill X::i in initializer of Y through constructor of X
-X
Jul 19 '05 #5

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

Similar topics

14
by: David Gausebeck | last post by:
The inability to denote data members as read-only in C++ is something which has annoyed me (slightly) for a while now. You can always get around it with accessor methods that return const...
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...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
14
by: Madhav | last post by:
hi all, I was trying the following code on the MS .net compiler: class Item { static int count; public: //static void init() { } Item()
12
by: Ivan Liu | last post by:
Hi, I wonder how I can declare a reference in private part of a class and use it in the member functions. I know I can do it by pointers but then I have to use pointer syntax, which I hope to...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
4
by: developereo | last post by:
Hi folks, Can anybody shed some light on this problem? class Interface { public: Interface() { ...} virtual ~Interface() { ...} virtual method() = 0; };
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
5
by: subramanian100in | last post by:
Consider the following classes without ctors. class Sample { private: double d_val; double* d_ptr; }; class Test
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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.