void and this

  • Thread starter Jonathan Mcdougall
  • Start date
J

Jonathan Mcdougall

I have a class member function declared as
class some_class {
...
virtual int call(void);

void in an empty parameter list is considered bad style in C++.
};

Can I use this-> inside the function body?

Of course, what makes you think you can't?


Jonathan
 
V

Vladimir Grul

Hello,

I have a class member function declared as

class some_class {
....
virtual int call(void);
};

Can I use this-> inside the function body?


Thanks.

Vladimir
 
A

Attila Feher

Vladimir said:
Hello,

I have a class member function declared as

class some_class {
...
virtual int call(void);

Drop the void. C++ is not C.

virtual int call(void);
and
virtual int call();
mean exactly the same
};

Can I use this-> inside the function body?

Yes.
 
V

Vladimir Grul

Jonathan Mcdougall said:
Of course, what makes you think you can't?

I was not sure if there is an implicit this in parameter list.

Thank you and Attila.

Vladimir
 
D

David B. Held

Attila Feher said:
Vladimir said:
Hello,

I have a class member function declared as

class some_class {
...
virtual int call(void);

Drop the void. C++ is not C.

virtual int call(void);
and
virtual int call();
mean exactly the same
[...]

So why does "(void)" mean "C"?

Dave
 
J

John Carson

David B. Held said:
Attila Feher said:
Vladimir said:
Hello,

I have a class member function declared as

class some_class {
...
virtual int call(void);

Drop the void. C++ is not C.

virtual int call(void);
and
virtual int call();
mean exactly the same
[...]

So why does "(void)" mean "C"?

Dave


If memory serves, in the C language, foo(void) means no arguments, whereas
foo() can mean none or one or ... any number of arguments. In C++, by
contrast, both foo(void) and foo() mean no arguments. Thus the void is
redundant in C++ but meaningful in C.
 
A

Attila Feher

David said:
Drop the void. C++ is not C.

virtual int call(void);
and
virtual int call();
mean exactly the same
[...]

So why does "(void)" mean "C"?

So why do you have to pick a quarrel on everything? Have you been bitten by
some vendetta bug?

To the OP:

As I have said above: the void is redundant, and has absolutely no effect on
a conforming compiler. And as everything redundant adding no value, the
only thing it can add is error, misunderstanding, misleading, wondering
collegagues why is it there and so forth. Since a virtual member function
needs no portability between C and C++ and the void there adds no value, it
is better be left out.
 
J

jeffc

Attila Feher said:
So why do you have to pick a quarrel on everything?
bwahahahaahahaha

As I have said above: the void is redundant, and has absolutely no effect on
a conforming compiler. And as everything redundant adding no value, the
only thing it can add is error, misunderstanding, misleading, wondering
collegagues why is it there and so forth.

Ridiculous. What POSSIBLE "misunderstanding" can come from putting the void
in there? It's no more "misleading" than the "public" here
struct A
{
public:
int i;
};

or the "private" here
class A
{
private:
int i;
};

Or the parentheses here
i = a + (b * c)
 
T

tom_usenet

Jonathan Mcdougall said:
I have a class member function declared as

class some_class {
...
virtual int call(void);

void in an empty parameter list is considered bad style in C++.
[...]

Since when?

Dennis Ritchie and Doug Ilroy described use of void to represent an
empty parameter list in C as an "abomination". Stroustrup agrees with
them, and void f(void); was illegal syntax in the early days of C++.
However it was reincorporated into C++ when C89 decided to use that
syntax in the interests of compatibility.

Tom
 
K

Kevin Goodsell

John said:
If memory serves, in the C language, foo(void) means no arguments, whereas
foo() can mean none or one or ... any number of arguments.

Actually, it means there is a fixed, but unspecified number of
arguments. You still have to pass exactly the right number and type of
arguments, but the compiler is unable to check whether you did. It's a
relic from before prototypes existed in C.

-Kevin
 
D

David B. Held

John Carson said:
[...]
If memory serves, in the C language, foo(void) means no
arguments, whereas foo() can mean none or one or ... any
number of arguments. In C++, by contrast, both foo(void)
and foo() mean no arguments. Thus the void is redundant
in C++ but meaningful in C.

I understand the distinction and the history, but I still fail to
understand why (void) is frowned upon or viewed as "C".
I happen to like the explicit way in which it says "this function
takes no arguments".

Dave
 
S

Sam Holden

John Carson said:
[...]
If memory serves, in the C language, foo(void) means no
arguments, whereas foo() can mean none or one or ... any
number of arguments. In C++, by contrast, both foo(void)
and foo() mean no arguments. Thus the void is redundant
in C++ but meaningful in C.

I understand the distinction and the history, but I still fail to
understand why (void) is frowned upon or viewed as "C".
I happen to like the explicit way in which it says "this function
takes no arguments".

Possibly because in C++ foo() explicitely says "this function takes
no arguments".

I personally find that foo() is more obvious than foo(void) when
quickly scanning code (as my brain sometimes sees the void and
has to double check to see if it is an argument type or not).

Of course my brain is defective in many ways, so that might just
be me...
 
E

E. Robert Tisdale

Vladimir said:
I have a class member function declared as

class some_class {
// ...
virtual int call(void);
};

Can I use this-> inside the function body?

Yes.
 
E

E. Robert Tisdale

David said:
I understand the distinction and the history
but I still fail to understand why
(void) is frowned upon or viewed as "C".

It us not.
I happen to like the explicit way in which it says
"this function takes no arguments".

I agree.
This is merely a style issue if you are writing C++ code.
If you are writing code that a C compiler must also be able to compile,
you should write:

int foo(void);

so that the C compiler can detect mistakes like

int k = foo("some string");

If you are in the habit of writing C++ code
that a C compiler must be able to compile,
using void to indicate an empty parameter list is a *good* habit.
 
D

David B. Held

Sam Holden said:
[...]
Possibly because in C++ foo() explicitely says "this function
takes no arguments".

I would say that that is an "implicit" form of saying that, but I
guess that's splitting hairs.
I personally find that foo() is more obvious than foo(void)
when quickly scanning code (as my brain sometimes sees
the void and has to double check to see if it is an argument
type or not).

And I find just the opposite. Of course, I always include an
access specifier for every group of members in a class as
well, so maybe I just like code to be more verbose.
Of course my brain is defective in many ways, so that might
just be me...

To each his own. When I see (int) 3.14, I think "C". But that's
because there's a *better* way to do it in C++. When I see
"(void)", I *don't* think "C", because the "C++ Way" isn't
technically better. It's just different.

Dave
 
D

David B. Held

WW said:
Stop trolling please.

[Earlier, you said...]
And as everything redundant adding no value, the only
thing it can add is error, misunderstanding, misleading,
wondering collegagues why is it there and so forth.

Can you spell "FUD"? Physician, heal thyself.

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top