Teen Programmers Unite  
 

 

Return to forum top

typedef'ing const pointer

Posted by bgbg_huji [send private reply] at December 03, 2002, 05:53:56 AM

Hi, all
let's say I have the following in c++ class declaration:

class A{....
public:....
struct i {
	vector<int>::iterator ii;
	vector<float>::iterator fi;
};
struct ci {
	vector<int>::const_iterator ii;
	vector<float>::const_iterator fi;
};
typedef i iterator;
typedef ci const_iterator;

const_iterator  f() const;
iterator f();
...};
now, when I say
A a;
...
A::const_iterator c;
c=a.f();

the compiler should execute the const version of f(), but in my case (gcc2.96) it doesn't. My question is: is it me, or is it my compiler?
Posted by taubz [send private reply] at December 03, 2002, 09:23:41 AM

I'm not sure I'm proficient at this level of C++, but at least in Java... I don't know how you can have two different definitions for f() that return two types. AFAIK, the function that is chosen by the compiler is the one that matches the arguments given, not the return type "expected."

- taubz

Posted by CodeRed [send private reply] at December 03, 2002, 11:44:11 AM

Are you sure you can do that? Maybe you know something that I don't but I don't think you can have two functions with the same name AND the same argument list. Now, if they had the same name but different arguments that would be function overloading, but that is not what you are doing.
I wrote and compiled this in VC++ 6:

#include <iostream.h>

int func();
float func();

int main()
{
int a;
float b;

a = func();
b = func();
cout << a << " " << b << endl;
}

int func()
{
	cout << "func1" << endl;
	return 1;
}

float func()
{
	cout << "func2" << endl;
	return 2;
}


and got this error:

"overloaded function differs only by return type"

???... I don't think it is allowed, sorry.
Posted by CodeRed [send private reply] at December 03, 2002, 11:46:42 AM

Just a thought, a way to get around this might be to pass the variable you want to store the results in by referance, which would include it in the argument list, making the two argument lists different between the two functions, which would make it a case of function overloading

Posted by CViper [send private reply] at December 03, 2002, 04:04:54 PM

You can have two "identical" functions, provided one is 'const', eg.

class A
{
    public:
        void F();
        void F() const;
};


However the const version of F() will only be called if the instance of the object you are calling it on also is const:
A a1;
const A a2;

a1.F(); // will call nonconst version of F()
a2.F(); // will call const version of F()


Return type dosen't matter...
Posted by gian [send private reply] at December 04, 2002, 01:45:38 AM

Sounds like you need a language with a cool parametric polymorphism implementation. *cough* SML *cough*.

Posted by regretfuldaydreamer [send private reply] at December 04, 2002, 02:33:58 PM

/me prefers the *cough* Why don't you try that out *cough* method of persuasion to Smerdyakovs brute force "TRY SML BEFORE YOU DIE OR YOU'LL GO TO HELL"

Posted by Psion [send private reply] at December 04, 2002, 05:45:08 PM

rdd, how about you write a web page summarizing the argument for using SML as I have and _then_ talk to me about "brute force"?

Posted by regretfuldaydreamer [send private reply] at December 04, 2002, 06:37:16 PM

Fair enough, SML is better than C and I've no problem with you promoting SML, just be a little more discrete like Gian... or any other sane person.

Posted by Psion [send private reply] at December 04, 2002, 07:10:14 PM

I'm not going to avoid giving sound advice because it might hurt some people's feelings with respect to their irrational attachments to bad practices.

Posted by gian [send private reply] at December 04, 2002, 11:54:25 PM

And it's not fair to ask Psion to be "sane", because he clearly isn't, that's why we love him :-)

Posted by CViper [send private reply] at December 05, 2002, 04:48:06 AM

I happen to like my "irrational attachments to bad practices", eg. programming in c/c++ :)

Posted by RedX [send private reply] at December 05, 2002, 12:51:52 PM

Hey, everybody must have a few bad habits. Else we would be perfect and would no longer excist. Beside C/C++ is the perfect language: 10 milion lemmings, heu, I mean C-programmers can't be wrong, can they?

Posted by regretfuldaydreamer [send private reply] at December 05, 2002, 01:22:34 PM

RedX: I haven't discovered the perfect language ... yet ... but I'm damn sure its not C. Since I haven't tried SML I've no complaint against it... my complaints against... <interpret the rest yourself>

Posted by CViper [send private reply] at December 05, 2002, 03:12:36 PM

Just out of curiosity: why is everybody bashing c/c++ here? Sure, it ain't 100% OO, but it does what you expect/has very few limitations, and is relativly easy to use (and everyone else is using it :D)

(please keep your reasons short and informative; don't want to start another flamewar here)

Posted by Neumann [send private reply] at December 05, 2002, 03:38:16 PM

http://www.schizomaniac.net/ml.html

Beating Psion at his own game.

This is Psion informative page about SML.

Posted by RedX [send private reply] at December 05, 2002, 04:03:18 PM

Regretfuldaydreamer, are you saying you missed the truckload of sarcasme in my post?

Posted by CodeRed [send private reply] at December 05, 2002, 06:23:25 PM

I got it, I even noticed the bad spelling and improper grammar ;)

Posted by RedX [send private reply] at December 06, 2002, 10:30:22 AM

Codered, my spelling and grammar were flawless. I checked it with my book "New Grammarr and Speling for the 43th century".

Posted by DragonWolf [send private reply] at December 09, 2002, 07:07:11 AM

Would it be more like?

|>15 15 7|-|3 1337 L4|\|6|_|463 0|= |>4 |=|_|7|_|R3

??

Posted by CViper [send private reply] at December 09, 2002, 02:24:48 PM

"mathematically defined (...) language"

does the above by DW classify as a mathematically defined language? :D

Posted by RedX [send private reply] at December 09, 2002, 03:11:15 PM

Nope, just plain old 13375p34|<

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.