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

constructor problem

84
I have a program which in a header file has a class of type:
Expand|Select|Wrap|Line Numbers
  1. class Perturbation: public otherclass{
  2. protected:
  3. (...)
  4. double *y;
  5. (..)
  6. public:
  7. Perturbation (Cosmos *c)
  8. (...)
  9.  
where Cosmos is another class (not important here)
When in the cc file the constructor is called it done in the following way:
Expand|Select|Wrap|Line Numbers
  1. Perturbation::Perturbation(Cosmos *c) : y(0)
  2.  
what does " : y(0)" mean?

Thanks very much for your help!
Jul 30 '07 #1
4 979
JonLT
41
.....
what does " : y(0)" mean?
.....
: y(0), means that the member y is initialized to 0, when the class constructor is called. You could also just write 'y = 0', in the constructor, only then y would be initialized a bit later.
Jul 30 '07 #2
sanctus
84
Since y is a pointer to a double does y(0) then mean that y is a pointer to a double initialized as zero (I mean the double it points to being zero)?

Also the use of () is not very clear to me here, I mean y[0] would be clear meaning the same value as *y...
Jul 30 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
what does " : y(0)" mean?
When an object is created, all of the data members must be created (and initialized) before you get to the opening brace of the constructor. The colon is the start of the initialization list. Here you can call constructors for your data members. In this case, a value of zero is to be placed into the y member when the y member is created.

y is a pointer to a double but as yet there is no double to point to so y has been initialized to zero.

Here's another example. A class Person that has a class Date as a member:
Expand|Select|Wrap|Line Numbers
  1. class Date
  2. {
  3.     public:
  4.        Date(int m, int d, int y);
  5. };
  6. class Person
  7. {
  8.      private:
  9.         Date birthdate
  10.         string name;
  11.      public:
  12.         Person(string n, int month, int day, int year);
  13. };
  14.  
When you create a Person object you want to initialize the Date member at the time the Date member is created. To do that yout need to call the Date constructor from the Person constructor and you need to initialize the string member so the string constructor needs to be called also:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.      Person p("John", 10, 31, 1975);
  4. }
  5.  
  6. //where the Person constructor is:
  7.  
  8. Person::Person(string n, int month, int day, int year) : birthdate(month, day, year), name(n)
  9. {
  10.  
  11. }
  12.  
}
Jul 30 '07 #4
sanctus
84
Thanks very much weaknessforcats that cleared it up!
Jul 30 '07 #5

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

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
6
by: Nafai | last post by:
Hello. I want to do something like this: class A { // It's virtual protected: float* data; int n; public: A(int a); virtual float* createData(); //...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
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...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.