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

variable declaration

Hello,

I would like to know the difference between two variable declaration
ways:

int point(0);

and

int point=0;

Thanks for your help!!

A.

Feb 28 '06 #1
5 2407
widmont posted:
Hello,

I would like to know the difference between two variable declaration
ways:

int point(0);

and

int point=0;

Thanks for your help!!

A.


When you're working with intrinsic types, there's no difference whatsoever.
It's a situation of "Thanks" Vs. "Thank you". Pick whichever tickles your
fancy.

Be careful though. If you want to "default initialise" something, then you
can't simply write:

int point();

That my friend is a function declaration. You can work your way around this
with:

int point = int();

But unfortunately, the type you're working with must have a public copy
constructor. So it won't work with "ostringstream":

ostringstream blah = ostringstream();
Fortunately though, if your type is a POD, you can do this:

PodType object = {};

(I'm open to correction on whether the above actually zero initializes
everything. If memory serves me right, then it does.)

When you have a contructor which takes more than one argument, then you
*have* to use the parenthesis form, eg.:

Dog benji("Benji",7);

You can't do:

Dog benji = "Benji", 7;
If you want to know how to default initialise something that has a non-
public copy constructor, then:

template<class T>
class DefaultInitialised : public T
{

DefaultInitialised() : T()
};

int main()
{
DefaultInitialised<ostringstream> k;
}
-Tomás
Feb 28 '06 #2
template<class T>
class DefaultInitialised : public T
{

DefaultInitialised() : T()
{

}
};

int main()
{
DefaultInitialised<ostringstream> k;
}


Feb 28 '06 #3
widmont wrote:
I would like to know the difference between two variable declaration
ways:

int point(0);

and

int point=0;


In this particular case (with 'int'), none.

V
--
Please remove capital As from my address when replying by mail
Feb 28 '06 #4
Tomás wrote:
widmont posted:
Hello,

I would like to know the difference between two variable declaration
ways:

int point(0);

and

int point=0;

Thanks for your help!!

A.
When you're working with intrinsic types, there's no difference
whatsoever. It's a situation of "Thanks" Vs. "Thank you". Pick whichever
tickles your fancy.

Be careful though. If you want to "default initialise" something, then you
can't simply write:

int point();

That my friend is a function declaration. You can work your way around
this with:

int point = int();

But unfortunately, the type you're working with must have a public copy
constructor. So it won't work with "ostringstream":

ostringstream blah = ostringstream();


No, but you can simply do:

ostringstream blah;

This will give you a default initialized variable, if the type is non-POD.
Fortunately though, if your type is a POD, you can do this:

PodType object = {};
I think this only works for compound types.
(I'm open to correction on whether the above actually zero initializes
everything. If memory serves me right, then it does.)
It default-initializes everything, which means zero for integer types.
When you have a contructor which takes more than one argument, then you
*have* to use the parenthesis form, eg.:

Dog benji("Benji",7);

You can't do:

Dog benji = "Benji", 7;
If you want to know how to default initialise something that has a non-
public copy constructor, then:

template<class T>
class DefaultInitialised : public T
{

DefaultInitialised() : T()
};
That constructor is private.
int main()
{
DefaultInitialised<ostringstream> k;
}


Feb 28 '06 #5
"Tom?s" <NU**@null.null> wrote:
When you have a contructor which takes more than one argument, then you
*have* to use the parenthesis form, eg.:

Dog benji("Benji",7);

You can't do:

Dog benji = "Benji", 7;


However, you can do:

Dog benji = Dog("Benji", 7);

(assuming the appropriate copy-constructors, etc., are accessible).

--
Marcus Kwok
Mar 2 '06 #6

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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: 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.