473,785 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constant member of a class

#include <iostream>
using namespace std;

class ConstantMember
{
const int m_const;
public:
ConstantMember( int cons = 10):m_const(con s){}
int getConst() const{ return m_const;}
};

int main()
{
ConstantMember X;//(20);
cout << X.getConst();
return 0;
}

the above is a program that was compiled & linked using g++. Is there
any other way to initialise a constant member variable using
constructor while creating objects?
Jul 4 '08
13 1656
Anarki wrote:
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>You used an initialiser list.
i understand i used initialiser list but whats happening inside the
initialiser list? What does the compiler do when it see the
initialiser list. I think u didn't get what i meant. i explain a bit
more what's the difference between a normal constructor and
constructor that uses initialiser list.
What does you book tell you? This is pretty basic stuff. If you want
to see exactly what your compiler does, look at the generated assembler.

--
Ian Collins.
Jul 14 '08 #11
On Jul 15, 1:08*am, Ian Collins <ian-n...@hotmail.co mwrote:
Anarki wrote:
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.co mwrote:
You used an initialiser list.
i understand i used initialiser list but whats happening inside the
initialiser list? What does the compiler do when it see the
initialiser list. I think u didn't get what i meant. i explain a bit
more what's the difference between a normal constructor and
constructor that uses initialiser list.

What does you book tell you? *This is pretty basic stuff. *If you want
to see exactly what your compiler does, look at the generated assembler.

--
Ian Collins.
Sorry Ian am not a good programmer. I dunno how to get my hands on the
generated assembler and if at all i get my hands on it i dont think i
can understand it. Anyways i am gonna search for the stuff Puppet_sock
said about utab.
Jul 15 '08 #12
Did u mean his post in this thread? if so

Does it really mean that initialization of constant member variable
occurs before the execution of constructor(usi ng initialiser list).

Again, does that mean a member variable of a object get initialized
even before the object is constructed?

Clearing basics of C++ looks real chaos(for me) :)
Jul 15 '08 #13
Anarki wrote:
Did u mean his post in this thread? if so
Please don't used txt speak on Usenet.
Does it really mean that initialization of constant member variable
occurs before the execution of constructor(usi ng initialiser list).
Yes, members in the initialisation list are initialised before the body
of the constructor is run.
Again, does that mean a member variable of a object get initialized
even before the object is constructed?
No, the initialisation is one phase of the object's construction.

--
Ian Collins.
Jul 15 '08 #14

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

Similar topics

2
6524
by: srinivas reddy | last post by:
Hi, Can a constant member function be overloaded with its non-constant equivalent. I mean can void func() be overloaded with void func() const. If so what behaviour is guaranteed, how does the compiler resolve ambiguity?, and how could I call a specific variant? tia, Srinivas
5
4834
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I missing something here and there is something I need to learn about? Clear, easy to understand...
8
2863
by: raan | last post by:
Microsoft C++ apparently doesn't permit me to initialize a static constant with in my class scope. Ok, I think that is previously discussed here and people have suggested some hacks to circumvent the issue. That being said, when you try to use that constant as in case expression things get wild. Here is my implementation. /*test.h*/ class test{
3
2111
by: archana | last post by:
Hi all, I have created one sealed class having one constant. Can i access that constant of sealed class without creating object of that class. If yes will internally new object gets created. Or will it behave like static member which remains in memory throughotut applications' lifetime. Please correct me if i am wrong.
3
20878
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my attempts so far have been unsuccessful. Take this Test class as an example // test.hpp
9
8895
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've *defined* "x"'s value to be 10, isn't above statement a
6
1688
by: Taras_96 | last post by:
Hi all, Jesse Liberty writes: "Note that the String class provides the operator+. The designer of the Employee class has blocked access to the operator+ being called on Employee objects by declaring that all the string accessors, such as GetFirstName(), return a constant reference. Because operator+ is not (and
4
1330
by: Gordon | last post by:
I'm trying to get a constant from a class, where the constant's name is known, but the class name isn't. I want to do things this way because I want classes to be able to define certain aspects for their setup themselves. For example: I have a situation where I have a script that can deal with objects of one of several classes, but each class needs some slightly different setup parameters. I'm currently taking care of this with a...
7
2813
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. Consider this stripped down example:
7
4269
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant expression") or Comeau Online ("constant value is not known"). If I replace
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.