Hello,
1) Should a standard compliant compiler accept the following code:
void Foo() { /* ... */ }
class Foo { /* ... */ };
int main()
{
Foo();
return 0;
}
If yes, what do you think the language allows it for? By the way, can
you predict what the program will do -- construct the object or call
the function?
2) Can you tell what exactly this short program will print?
#include<iostream>
using std::cout;
class MyClass {
public:
MyClass() : i_(2) {}
MyClass(const MyClass& obj) { i_ *= obj.i_; }
~MyClass() { cout << i_ << std::endl; }
private:
int i_;
};
int main()
{
MyClass( MyClass() );
return 0;
}
-Alexander 15 1991
On 12 Jul 2003 07:28:49 -0700, ko******@rambler.ru (Alexander Korovyev) wrote: 1) Should a standard compliant compiler accept the following code:
void Foo() { /* ... */ } class Foo { /* ... */ };
int main() { Foo(); return 0; }
If yes, what do you think the language allows it for? By the way, can you predict what the program will do -- construct the object or call the function?
If this was a real question the advice would be to use
different names.
But as a HOMEWORK PROBLEM the advice is to (1) check your
textbook, and (2) check the standard. Find the relevant
pages. In your answer, don't answer just yes or no, and
don't just quote an authoritative source, but also include
the reasoning _why_ it is like it is.
2) Can you tell what exactly this short program will print?
#include<iostream> using std::cout;
class MyClass { public: MyClass() : i_(2) {} MyClass(const MyClass& obj) { i_ *= obj.i_; } ~MyClass() { cout << i_ << std::endl; } private: int i_; };
int main() { MyClass( MyClass() ); return 0; }
Why don't you try to compile it, and if it compiles, run it,
and if it runs and you don't understand it, trying it in
a debugger?
Note: that a program compiles with one specific compiler and
compiler options doesn't mean it has defined behavior according
to the standard, but either way you know a lot more.
But you didn't even try, so, are you stupid?
"Alexander Korovyev" <ko******@rambler.ru> wrote in message
news:26**************************@posting.google.c om... Hello,
1) Should a standard compliant compiler accept the following code:
void Foo() { /* ... */ } class Foo { /* ... */ };
int main() { Foo(); return 0; }
My compiler, VC++6, compiles it fine. Of course, VC++ is known for its
non-standard conventions, but it seems like it is perfectly legal, and
should be. If yes, what do you think the language allows it for?
It's the same reason you could use Foo() and foo() to mean two different
functions. It just gives you more names you could use. As it will always be
unambiguous to what you mean, it's ok to do this.
By the way, can you predict what the program will do -- construct the object or call the function?
I just said it's always unambiguous. Thus, reading into your code a bit will
tell you. It could not possibly be constructing the object as there is no
variable there, so there is nothing to construct. It is, therefore, calling
the function. IIRC, the defauly constructor, in this case, would have to be
called as Foo::Foo(), to make it unambiguous. 2) Can you tell what exactly this short program will print?
#include<iostream> using std::cout;
class MyClass { public: MyClass() : i_(2) {} MyClass(const MyClass& obj) { i_ *= obj.i_; } ~MyClass() { cout << i_ << std::endl; } private: int i_; };
int main() { MyClass( MyClass() ); return 0; }
-Alexander
Well my compiler prints 2. IMHO, you're lucky, though. It appears to me
that:
1) the first constructor that's called makes its i_ 2.
2) the copy constructor then takes its i_ and multiplies it by 2. As its i_
is undefined, it appears that this code is also undefined.
Then again, here's another way of looking at it:
MyClass(MyClass());
As the constructor(s) don't return a value, this is the same as saying
MyClass(void);
Which would be the same as saying
MyClass();
Which would make i_ 2.
Then again, as two constructors were called, shouldn't there be two 2s on
output? I only saw one. Perhaps the compiler optimized it out??
That's really confusing, but is this a homework assignment or something? Are
you curious? This seems to have no practical purpose to me...
--
MiniDisc_2k2
"Alf P. Steinbach" wrote:
[...] But you didn't even try, so, are you stupid?
Careful. Alf.
regards,
alexander.
On Sat, 12 Jul 2003 17:08:40 +0200, Alexander Terekhov <te******@web.de> wrote: "Alf P. Steinbach" wrote: [...] But you didn't even try, so, are you stupid?
Careful. Alf.
I don't like homework questions posted in disguise.
If you do, feel free to provide step-by-step explanations of
such questions.
MiniDisc_2k2 wrote: Then again, here's another way of looking at it:
MyClass(MyClass());
As the constructor(s) don't return a value, this is the same as saying
MyClass(void);
Which would be the same as saying
MyClass();
Which would make i_ 2.
Then again, as two constructors were called, shouldn't there be two 2s on output? I only saw one. Perhaps the compiler optimized it out??
That's really confusing, but is this a homework assignment or something? Are you curious? This seems to have no practical purpose to me...
Then g++ thinks that "MyClass( MyClass() );" declares a function MyClass
that returns a MyClass and prints nothing at all.
int main()
{
MyClass( MyClass() ); // declare function MyClass
::MyClass xx( MyClass() ); // call function MyClass
return 0;
}
And who thought this was a good idea ?
"Alf P. Steinbach" wrote: On Sat, 12 Jul 2003 17:08:40 +0200, Alexander Terekhov <te******@web.de> wrote:
"Alf P. Steinbach" wrote: [...] But you didn't even try, so, are you stupid? Careful. Alf.
I don't like homework questions posted in disguise.
Are you stupid? If you do, feel free to provide step-by-step explanations of such questions.
Nah. That would make many professors 'get promoted'.
regards,
alexander.
On Sat, 12 Jul 2003 19:16:15 +0200, Alexander Terekhov <te******@web.de> wrote: "Alf P. Steinbach" wrote: On Sat, 12 Jul 2003 17:08:40 +0200, Alexander Terekhov <te******@web.de> wrote:
> >"Alf P. Steinbach" wrote: >[...] >> But you didn't even try, so, are you stupid? > >Careful. Alf.
I don't like homework questions posted in disguise.
Are you stupid?
You're trolling again. Find a cold beer, go out in the
sunshine, enjoy life. Don't spill beer in the keyboard.
On Sat, 12 Jul 2003 19:28:50 +0200, Alexander Terekhov <te******@web.de> wrote: "Alf P. Steinbach" wrote: [...] You're trolling again.
As always.
Yep, suspected that. Find a cold beer, go out in the sunshine, enjoy life. Don't spill beer in the keyboard.
Heck. I'm right now drinking "Tisserand" (not a beer and no, I have no problems with the keyboard).
Enjoy your "Tisserand", unless it's aromatic oil (ugh) [that's
what google insists on].
"Gianni Mariani" <gi*******@mariani.ws> wrote... MiniDisc_2k2 wrote: Then again, here's another way of looking at it:
MyClass(MyClass());
As the constructor(s) don't return a value, this is the same as saying
Constructors return objects. They don't have a return value
types in a regular sense because they cannot be called.
However, the syntax <type-name>() _usually_ creates a temporary
of that type and default-initialises it. MyClass(void);
Which would be the same as saying
MyClass();
Which would make i_ 2.
Then again, as two constructors were called, shouldn't there be two 2s
on output? I only saw one. Perhaps the compiler optimized it out??
That's really confusing, but is this a homework assignment or something?
Are you curious? This seems to have no practical purpose to me... Then g++ thinks that "MyClass( MyClass() );" declares a function MyClass that returns a MyClass and prints nothing at all.
int main() { MyClass( MyClass() ); // declare function MyClass
There are no implicit return types in C++. If 'MyClass hasn't
been defined, the statement above is a syntax error. If g++ allows
it, g++ is simply wrong, then.
OTOH, if 'MyClass' is a type-id, then it's a creation of
a temporary from another temporary. Both are discared at ';'. ::MyClass xx( MyClass() ); // call function MyClass
return 0; }
And who thought this was a good idea ?
Victor Bazarov wrote: "Gianni Mariani" <gi*******@mariani.ws> wrote... int main() { MyClass( MyClass() ); // declare function MyClass
There are no implicit return types in C++. If 'MyClass hasn't been defined, the statement above is a syntax error. If g++ allows it, g++ is simply wrong, then.
OTOH, if 'MyClass' is a type-id, then it's a creation of a temporary from another temporary. Both are discared at ';'.
::MyClass xx( MyClass() ); // call function MyClass
return 0; }
And who thought this was a good idea ?
I'm pretty sure you missed the point. I HAVE NO IDEA if this is correct
C++ but the gcc 3.3 compiler recognizes this:
MyClass( MyClass() );
as a declaration of a function called "MyClass" taking no parameters and
returning a MyClass object.
If you then try to calling it like so:
::MyClass xx( MyClass() );
It will actually call function "MyClass()".
Hey, what happened to my kill file.
Go figure.
"Gianni Mariani" <gi*******@mariani.ws> wrote... Victor Bazarov wrote: "Gianni Mariani" <gi*******@mariani.ws> wrote...
int main() { MyClass( MyClass() ); // declare function MyClass
There are no implicit return types in C++. If 'MyClass hasn't been defined, the statement above is a syntax error. If g++ allows it, g++ is simply wrong, then.
OTOH, if 'MyClass' is a type-id, then it's a creation of a temporary from another temporary. Both are discared at ';'.
::MyClass xx( MyClass() ); // call function MyClass
return 0; }
And who thought this was a good idea ?
I'm pretty sure you missed the point. I HAVE NO IDEA if this is correct C++ but the gcc 3.3 compiler recognizes this:
MyClass( MyClass() );
as a declaration of a function called "MyClass" taking no parameters and returning a MyClass object.
I probably missed the point. And, yes, it's not correct C++.
Victor ko******@rambler.ru (Alexander Korovyev) wrote in message news:<26**************************@posting.google. com>... 1) Should a standard compliant compiler accept the following code:
void Foo() { /* ... */ } class Foo { /* ... */ };
int main() { Foo(); return 0; }
If anything in this world can answer your question correctly, this is it:
<http://www.comeaucomputing.com/tryitout>
If yes, what do you think the language allows it for?
C compatibility. The following is the POSIX way for getting the size of a file:
struct stat st;
if (stat(filename, &st)) printf("%d\n", st.st_size);
(Microsoft calls them "_stat" and "struct _stat", respectively.)
By the way, can you predict what the program will do -- construct the object or call the function?
Yes, I can. You can, too, if you read carefully.
2) Can you tell what exactly this short program will print?
#include<iostream> using std::cout;
class MyClass { public: MyClass() : i_(2) {} MyClass(const MyClass& obj) { i_ *= obj.i_; } ~MyClass() { cout << i_ << std::endl; } private: int i_; };
int main() { MyClass( MyClass() ); return 0; }
No, nor can anyone else.
- Shane
"MiniDisc_2k2" <Ma******@nospam.com> wrote in message news:<J8VPa.2454$zd4.764@lakeread02>...
[skipped] Then again, as two constructors were called, shouldn't there be two 2s on output? I only saw one. Perhaps the compiler optimized it out??
That's really confusing, but is this a homework assignment or something? Are you curious? This seems to have no practical purpose to me...
The question arose from reading Andrei Alexandrescu's "Modern C++
Design". In section 2.1 he describes a technique of compile-time
assertion checking:
template<bool> struct CompileTimeChecker
{
CompileTimeChecker(…);
};
template<> struct CompileTimeChecker<false> {};
#define STATIC_CHECK(expr, msg) \
{\
class ERROR_##msg{}; \
(void)sizeof(CompileTimeChecker<(expr) != 0>(ERROR_##msg())); \
}
where CompileTimeChecker<(expr) != 0>(ERROR_##msg()) is intended by
the author to evaluate in an object. Believe it or not, but the latest
Microsoft compiler interprets it as a function pointer declaration!
Likewise, MyClass(MyClass()) line in question 2) of my posting is
interpreted not as a successive construction of two automatic objects,
but as the MyClass (*MyClass)() declaration. This unfortunate
situation IMHO could not be possible if C++ did not effectively add
extra headache by admitting the collision described in question 1) (or
doesn't it really admit so?). So the reason I asked was to clarify
whether it was Mr. Alexandrescu's mistake due to the too much
complexity of C++ syntax or the shortcoming of my compilers.
-Alexander sb******@cs.uic.edu (Shane Beasley) wrote in message news:<2f**************************@posting.google. com>... ko******@rambler.ru (Alexander Korovyev) wrote in message news:<26**************************@posting.google. com>...
[skipped] 2) Can you tell what exactly this short program will print?
#include<iostream> using std::cout;
class MyClass { public: MyClass() : i_(2) {} MyClass(const MyClass& obj) { i_ *= obj.i_; } ~MyClass() { cout << i_ << std::endl; } private: int i_; };
int main() { MyClass( MyClass() ); return 0; }
No, nor can anyone else.
Well, it is clear there are at most only two possibilities -- the
program output is either undefined or is empty. Is there really a set
of C++ rules implying that
MyClass( MyClass() );
can never be a function pointer declaration?
-Alexander This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Neil Zanella |
last post by:
Hello,
I would like to ask how come the design of C++ includes
std::pair. First of all I don't think many programmers
would use it. For starters, what the first and second
members are depends...
|
by: JustSomeGuy |
last post by:
in the stl map class I see the use of a function pair and make_pair.
What is the difference between pair and make_pair?
dictionary.insert(std::pair<Key, Value>(k,v));
works as well as:
...
|
by: pmatos |
last post by:
Hi all,
Is there a way of (after creating a pair) set its first and second
element of not? (pair is definitely a read-only struct)?
Cheers,
Paulo Matos
|
by: Allerdyce.John |
last post by:
I have a vector of Pair of int:
typedef pair<int, int> MyPair;
typedef vector <MyPair > MyVector
I would like to remove entries if their first are equal, or if their
value is swap ( first of...
|
by: Florent Garcin |
last post by:
Hello!
I would like to use the map structure with a key of Pair<string, string>
and an int as the value.
Pair is defined as:
template <class T1, class T2>
class Pair
{
|
by: kietzi |
last post by:
Hello world,
I was wondering whether it would be possible to create a map which uses
a pair of ints as key and a float as value. I have used maps with const
char* as key, but have so far not been...
|
by: onkar |
last post by:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*arr={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const...
|
by: desktop |
last post by:
I have made this little test with std::pair:
test<intt1(1);
test<intt2(2);
std::pair<test<int>,test<int mypair = std::make_pair(t1,t2);
where test is a template class I wrote. It seems a bit...
|
by: Alex Vinokur |
last post by:
Hi,
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Alex Vinokur
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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: 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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |