What is the purpose of a const in the prototype for a non member method

P

parag_paul

I have a non member function , which has a prototype

const print() const{

What could be the purpose of the same?
 
U

Uday Bidkar

I have a non member function , which has a prototype

const print() const{

What could be the purpose of the same?

You can not use function modifiers like const, volatile on nonmember
functions. It won't allow your code to compile.

-Uday Bidkar
 
P

parag_paul

#include<iostream>
using namespace::std;

const void print () {
cout << " wow "<< endl;
}

int main(){
print();
return 0;
}


But I compile the above with g++ 3.6 and it does work for me.
without any warnings. i think we might have misunderstanding
-Parag
 
P

parag_paul

I am sorry about the const modifier declaration after the method.
This was not intended. I am talking about the initial one
 
D

Daniel T.

#include<iostream>
using namespace::std;

const void print () {
cout << " wow "<< endl;
}

int main(){
print();
return 0;
}


But I compile the above with g++ 3.6 and it does work for me.
without any warnings. i think we might have misunderstanding
-Parag

Maybe some realistic examples will help:

struct Foo {
int i;
};

const Foo cbar() { return Foo(); }
Foo bar() { return Foo(); }

int main() {
int i = ++bar().i; // compiles fine
i = ++cbar().i; // doesn't compile
}

When the return is non-const, the value returned can be modified. Note,
however that this is only the case with user defined types, if 'bar()'
and 'cbar()' returned integral types (e.g., int, double) then the
'const' would be superfluous. In your example "const void", the const is
also pointless because you can't modify a void in any case. :)

There is a guideline that whenever a function returns a user defined
type by value, it should actually be modified by const. That way they
behave more like the integral types. Chances are, you are looking at
code where someone over applied that guideline.
 
R

red floyd

I have a non member function , which has a prototype

const print() const{

What could be the purpose of the same?

This has got to be a homework question. I've seen the exact same
question asked four or five times within the past week.
 
R

red floyd

I am sorry about the const modifier declaration after the method.
This was not intended. I am talking about the initial one

It's a syntax error.

const print() const;

has no return type, and implicit int is illegal.
 
J

James Kanze

#include<iostream>
using namespace::std;
const void print () {
cout << " wow "<< endl;
}
int main(){
print();
return 0;
}
But I compile the above with g++ 3.6 and it does work for me.
without any warnings. i think we might have misunderstanding

It's legal. Void is the basic type of the return value, and you
are allowed to add cv qualifiers to the type of the return
value. They only have any real meaning if the return value is a
class type; a function return is an rvalue, and non-class type
rvalues never have a cv qualified type, regardless of the
declaration. Thus, "int const f()" and "int f()" both declare a
function returning an int (and not an int const); "ClassType
const f()" and "ClassType f()", on the other hand, return
different types. (This is my interpretation of the standard,
but I don't think that the standard is as clear as it could be
here. Both g++ and Sun CC do treat "int const f()" and "int
f()" as having different return types. Although §3.10/9 clearly
says "Class rvalues can have cv-qualified types; non-class
rvalues always have cv-unqualified types.")

You wouldn't normally write something like the above, but such
things might easily occur in the instantiation of a template,
e.g.:

template< typename T >
T const f( T* p ) { ... } ;

Call this function with a void const*, and it will return a void
const.
 
J

James Kanze

template< typename T >
T const f( T* p ) { ... } ;
I dont understand the above,
int const f(int* p)

That's something different. That function will always return an
int const---which is the same as an int, given that const is
ignored on non-class type rvalues.
How to call this with a void const * , I also have never
seen a const*

For my function, it's easy:

void const* p = NULL ;
f( p ) ;

calls the function with a void const*. As for the const*, in
isolation, of course, it doesn't mean anything. The const
applies to whatever is to the left of the const.
 

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
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top