By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

const& typecast

P: n/a
After more searching I found that defining const operators
can cause problems with many compilers due to the way it
interprets the C++ standard. I removed the const
operators from my class and it seems to be working fine
now.
-----Original Message-----
I have C++ code that has been running just fine in VS6 fora couple of years. I brought it into VS.NET with managed
extensions turned off and it compiles/links just fine,
however I get access violations at runtime. The code in
question is basically a smart pointer which has the type
cast operators overloaded. There is both a non-const and
const operator defined. The non-const type cast works,
the const type cast does not. I am including a simple
example that exhibits the same behavior. Thanks in
advance for looking at this. I'm stumped.
#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

//////////////////////////////////////////
//////////////////////////////////////////
class Foo
{
public:
Foo();
Foo(const Foo& foo);
string Name() const;
void Name(string name);
private:
string name_m;
};

Foo::Foo()
{
name_m = "";
}

Foo::Foo(const Foo& foo)
{
name_m = foo.Name();
}

string Foo::Name() const
{
return name_m;
}
void Foo::Name(string name)
{
name_m = name;
}

//////////////////////////////////////////
//////////////////////////////////////////
class Bar
{
public:
Bar();
Bar(const Bar& bar);
string Name() const;
void Name(string name);
private:
string name_m;
};

Bar::Bar()
{
name_m = "";
}

Bar::Bar(const Bar& bar)
{
name_m = bar.Name();
}

string Bar::Name() const
{
return name_m;
}
void Bar::Name(string name)
{
name_m = name;
}

//////////////////////////////////////////
//////////////////////////////////////////
class FooBar
{
public:
FooBar();
void SetFoo(Foo& foo);
void SetBar(Bar& bar);
operator Foo& () const;
operator const Foo& () const;
operator Bar& () const;
operator const Bar& () const;
private:
Foo* foo_m;
Bar* bar_m;
};

FooBar::FooBar()
{
foo_m = NULL;
bar_m = NULL;
}

void FooBar::SetFoo(Foo& foo)
{
foo_m = new Foo(foo);
}

void FooBar::SetBar(Bar& bar)
{
bar_m = new Bar(bar);
}

FooBar::operator Foo& () const
{
return *foo_m;
}

FooBar::operator const Foo& () const
{
return *foo_m;
}

FooBar::operator Bar& () const
{
return *bar_m;
}

FooBar::operator const Bar& () const
{
return *bar_m;
}

//////////////////////////////////////////
//////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
Foo foo;
Bar bar;
foo.Name("foo");
bar.Name("bar");

cout << "foo.Name() = " << foo.Name() << endl;
cout << "bar.Name() = " << bar.Name() << endl;

FooBar fooBar;
fooBar.SetFoo(foo);
fooBar.SetBar(bar);

cout << "((Foo&)fooBar).Name() = " << ((Foo&)
fooBar).Name() << endl;
cout << "((Bar&)fooBar).Name() = " << ((Bar&)
fooBar).Name() << endl;

cout << "((const Foo&)fooBar).Name() = " << ((const
Foo&)fooBar).Name() << endl;
cout << "((const Bar&)fooBar).Name() = " << ((const
Bar&)fooBar).Name() << endl;

return 0;
}
.

Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.