473,382 Members | 1,441 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.

C++ syntax, "MyClass m;" versus "MyClass m();"

Hi!

When defining a new object m of class MyClass, with no parameters to
the constructor, one is supposed to write "MyClass m;". If one wants to
pass a parameter p to the constructor, one is supposed to write
"MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
wants to pass zero parameters. Why is that? It seems inelegant and
non-symmetric to me. What reason is there behind this?

/ Me, seeking insights into C++

Jan 19 '07 #1
3 1519
On Fri, 19 Jan 2007 20:12:44 +0800, <ja****@gmail.comwrote:

"MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
wants to pass zero parameters. Why is that? It seems inelegant and
if you write "MyClass m()",
it is not a Class,it is a function declare,
it means:this is a function ,its name is m,its param is void,and it
returns sth with type MyClass.
think about this:
you can declare a function like this:int func(int a)
also you can :int func(int)

--
Hello,World!
----legolaskiss.
Jan 19 '07 #2
ja****@gmail.com wrote:
Hi!

When defining a new object m of class MyClass, with no parameters to
the constructor, one is supposed to write "MyClass m;". If one wants to
pass a parameter p to the constructor, one is supposed to write
"MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
wants to pass zero parameters. Why is that? It seems inelegant and
non-symmetric to me. What reason is there behind this?
You're right in that it is kind of unintuitive. The reason is that
"MyClass m();" already has another meaning. It declares a function taking no
arguments and returnint MyClass, similar to "int myfunc();".

Jan 19 '07 #3

Rolf Magnus wrote:
ja****@gmail.com wrote:
Hi!

When defining a new object m of class MyClass, with no parameters to
the constructor, one is supposed to write "MyClass m;". If one wants to
pass a parameter p to the constructor, one is supposed to write
"MyClass m(p);". It doesn't work if one writes "MyClass m();" when one
wants to pass zero parameters. Why is that? It seems inelegant and
non-symmetric to me. What reason is there behind this?

You're right in that it is kind of unintuitive. The reason is that
"MyClass m();" already has another meaning. It declares a function taking no
arguments and returnint MyClass, similar to "int myfunc();".
See a message related to the issue:
http://groups.google.com/group/comp....07f633d38f2f9f

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jan 19 '07 #4

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

Similar topics

2
by: DraguVaso | last post by:
Hi, I have a simple question regardign the syntax: do I have to need double quotation marks or single qoutation marks in my XML? For exemple:µ <?xml version="1.0" or <?xml version='1.0'
2
by: Optimus | last post by:
Hi everyone, I got a question about a syntax in c# which I'm not familiar with. And then I've got some sample source code as shown below. public class Window: System.Windows.Forms.Form {...
16
by: Gernot Frisch | last post by:
Hi, class MyString { char* m_data; public: MyString(const char* c) { m_data = new char; strcpy(m_data, c);
4
by: chaitu | last post by:
Hi guys, I've written a parallel build program (in Perl) that takes a pre-computed dependency tree of many projects in 2 visual studio .net 2003 solution (.sln) files here at my company, and...
10
by: jonathanemil | last post by:
Hello, I am a 1st semester Computer Science student in a Python class. Our current assignment calls for us to read a list from a file, create a 2-dimensional list from the file, and check to see...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
3
by: dwblas | last post by:
The annual Linux Journal survey is online now for any Linux users who want to vote for Python. http://www.linuxjournal.com/node/1006101
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.