Angus
Angus

Reputation: 12621

const_cast in c++ -getting errors

include "stdafx.h"
#include <iostream>
using namespace std;

class Foo{
public:
    void func()
    {
        cout<<"Hello!!"<<endl;
    }
};

void some_func(const Foo &f)
{
    //f.func();
    Foo &fr=const_cast<Foo&>(f);
    fr.func();
}
int main()
{
    some_func(Foo &f); //if const declared will add the no of errors from 2 to 3
    return 0;
}

How to invoke the some_func(const Foo &f)...If i declare the const before Foo parameter in main it shows me error... But if i'm using the code above i'm getting 2 errors..

output:

1>------ Build started: Project: const_cast, Configuration: Debug Win32 ------
1>Compiling...
1>const_cast.cpp
1>c:\documents and settings\beata\my documents\visual studio 2008\projects\const_cast\const_cast\const_cast.cpp(24) : error C2065: 'f' : undeclared identifier
1>c:\documents and settings\beata\my documents\visual studio 2008\projects\const_cast\const_cast\const_cast.cpp(24) : error C2275: 'Foo' : illegal use of this type as an expression
1>        c:\documents and settings\beata\my documents\visual studio 2008\projects\const_cast\const_cast\const_cast.cpp(8) : see declaration of 'Foo'
1>Build log was saved at "file://c:\Documents and Settings\beata\My Documents\Visual Studio 2008\Projects\const_cast\const_cast\Debug\BuildLog.htm"
1>const_cast - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Upvotes: 1

Views: 1211

Answers (5)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361332

some_func(Foo &f); //if const declared will add the no of errors from 2 to 3

Wrong syntax.

Here is what you should be doing:

Foo f; //f is an object of type Foo
some_func(f); //pass the object to the function.

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 791651

some_func(Foo &f); looks something like a declaration and something like a function call. If you meant a function call you just pass an object of the appropriate type to the function. E.g.

Foo f;
some_func(f);

or if you want to pass an unnamed temporary (legal because the function takes a const reference):

some_func(Foo());

Upvotes: 2

wheaties
wheaties

Reputation: 35970

The problen you're seeing is that you haven't labled the func function call as const to indicate to the compiler that it does not modify visible state. That is,

class Foo{
public:
    void func() const{
        std::cout << "Hello World!" << std::end;
    }
};

will work fine. You put the const at the end of the function calls when they do not modify state (not completely true but more advanced for this post.)

So if you want to pass an object by const ref, you will only ever be able to call methods on it that have been declared non-state modifying. Please don't use const_cast unless you absolutely have to.

Also, don't forget to declare a variable of type Foo in your main body.

Upvotes: 1

sehe
sehe

Reputation: 392911

int main()
{
    Foo f;
    some_func(f);
    return 0;
}

Upvotes: 2

ta.speot.is
ta.speot.is

Reputation: 27214

Your problem is probably with main()

int main()
{
    Foo f;
    some_func(f);
    return 0;
}

You need to declare f before you can use it.

Upvotes: 2

Related Questions