473,322 Members | 1,734 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,322 software developers and data experts.

Constant member of a class

#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;}
};

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 #1
13 1613
On Fri, 04 Jul 2008 02:08:13 -0700, Anarki wrote:
#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;}
};

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?
There are two points: initialization and constructor body execution, the
reason for const member functions to be initialized in the initializer is
that by the time the constructor body is executed, the initialization is
complete, and for const members, this is not possible because once you
assign the value you can not change it afterwards.

HTH
Jul 4 '08 #2
There are two points: initialization and constructor body execution, the
reason for const member functions to be initialized in the initializer
Of course "const members" not const member functions
Jul 4 '08 #3
On Jul 4, 5:08 am, Anarki <Deepchan...@gmail.comwrote:
#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;}

};

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?

well yes, copy ctor which is generated for you in the code above and
should look something like ...

class C
{
const int m;
public:
C(int n = 10) : m(n) { }
C(const C& copy) : m(copy.m) // ... this
{
std::cout << "copy\n";
}
int get() const { return m; }
};

int main()
{
C c;
std::cout << c.get() << std::endl;
C cop(c);
std::cout << cop.get() << std::endl;
}

/*
10
copy
10
*/
Jul 4 '08 #4
#include <iostream>
using namespace std;

class ConstantMember1
{
const int m_const;
public:
ConstantMember1(int cons = 20){m_const = cons;}
int getConst() const{ return m_const;}

};

int main()
{
ConstantMember1 Y;
cout << X.getConst();
return 0;

}
Why doesn't this program compile? What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )
Jul 14 '08 #5
On Jul 14, 3:07*pm, Anarki <Deepchan...@gmail.comwrote:
#include <iostream>
using namespace std;

class ConstantMember1
{
* * * * const int m_const;
* * * * public:
* * * * ConstantMember1(int cons = 20){m_const = cons;}
* * * * int getConst() const{ return m_const;}

};

int main()
{
* * * * ConstantMember1 Y;
* * * * cout << X.getConst();
* * * * return 0;

}

Why doesn't this program compile? What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )
sorry there is a typing mistake in the above code its Y.getConst();
but i have typed X.getConst();

Guys is there way to edit my post where i have made mistakes like the
above?
Jul 14 '08 #6
Anarki wrote:
#include <iostream>
using namespace std;

class ConstantMember1
{
const int m_const;
public:
ConstantMember1(int cons = 20){m_const = cons;}
int getConst() const{ return m_const;}

};

int main()
{
ConstantMember1 Y;
cout << X.getConst();
return 0;

}
Why doesn't this program compile?
Too many bugs? Your compiler errors should have been clear enough.

The only way to initialise a const member is with an initialiser list.
What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )
You used an initialiser list.

--
Ian Collins.
Jul 14 '08 #7
Anarki wrote:
>
Guys is there way to edit my post where i have made mistakes like the
above?
No. Cut and paste code you have tried rather than typing it into the
message.

--
Ian Collins.
Jul 14 '08 #8
On Jul 14, 3:16*pm, Ian Collins <ian-n...@hotmail.comwrote:
Anarki wrote:
#include <iostream>
using namespace std;
class ConstantMember1
{
* * * * const int m_const;
* * * * public:
* * * * ConstantMember1(int cons = 20){m_const = cons;}
* * * * int getConst() const{ return m_const;}
};
int main()
{
* * * * ConstantMember1 Y;
* * * * cout << X.getConst();
* * * * return 0;
}
Why doesn't this program compile?

Too many bugs? *Your compiler errors should have been clear enough.
look friend i know the error is due to the constant variable in class.
But why should the compiler raise error? Am looking for more detailed
explanation.
>
The only way to initialise a const member is with an initialiser list.
What is the difference between the
constructor i gave in first post and this ( ConstantMember() and
ConstantMember1() )

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.
>
--
Ian Collins.
The answer you gave me was just the technical term/terminology of the
example i posted. But what exactly is happening inside this
initialiser list?? Some one please help......
Jul 14 '08 #9
On Jul 14, 10:07*am, Anarki <Deepchan...@gmail.comwrote:
[snip]
Why doesn't this program compile?
Too many bugs? *Your compiler errors should have been clear enough.

look friend i know the error is due to the constant variable in class.
But why should the compiler raise error? Am looking for more detailed
explanation.
Did you read the post by utab that appeared here 10 days ago?
It's still on Google, so you could find it if you looked.
Socks
Jul 14 '08 #10
Anarki wrote:
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.comwrote:
>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.comwrote:
Anarki wrote:
On Jul 14, 3:16 pm, Ian Collins <ian-n...@hotmail.comwrote:
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(using 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(using 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
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...
5
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...
8
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...
3
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....
3
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...
9
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...
6
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...
4
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...
7
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...
7
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.