473,756 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2424
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 DefaultInitiali sed : public T
{

DefaultInitiali sed() : T()
};

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

DefaultInitiali sed() : T()
{

}
};

int main()
{
DefaultInitiali sed<ostringstre am> 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 DefaultInitiali sed : public T
{

DefaultInitiali sed() : T()
};
That constructor is private.
int main()
{
DefaultInitiali sed<ostringstre am> 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
6517
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
7
2677
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 before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the beginning of a method or whether they will allocate the memory as it is needed by the method to...
2
8832
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 { public: Book()
134
7904
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 means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
4
2719
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 function, it is global regardless of whether it's declared with or without "var" * When it is declared inside a function, if declared with "var", it's local, if not, it's global
14
13060
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
2
4716
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 declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
1
11865
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; Public; Global; etc) will cause an error, either at compile time or when attempting to execute the procedure it's referred to from within. With this unset, any unregognised references will be treated as a previously unknown and unset variable of type...
112
5468
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 that may print some messages. foo(...) { if (!silent)
11
2927
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 this possible.I thought it will throw "Variable redefinition Error". Giving the source code for reference.Please help me out on this... a.h ----- int g; void fun(void);
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
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
6534
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
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.