FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

typeof() problems

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
JoeCoder



Joined: 29 Oct 2005
Posts: 294

PostPosted: Wed Jun 07, 2006 1:33 pm    Post subject: typeof() problems Reply with quote

I'm having trouble with typeof(). In the simplified example below, I want methodB initialize c as a new B() and not a new A(). typeof seems to return the type of whatever's pointing to the class and not the class itself. I could use classinfo.name, but I was wondering if there's an easier way. Here's my code:
Code:
class A
{  void print()
   {  printf("A.print()\n");
   }
}

class B : A
{   void print()
   {  printf("B.print()\n");
   }
   void methodB(A a)
   {  typeof(a) c = new typeof(a);
      c.print();
   }
}

void main()
{  B b = new B();
   b.methodB(new B());
}
Back to top
View user's profile Send private message
yossarian



Joined: 21 Jun 2006
Posts: 8

PostPosted: Fri Jun 23, 2006 6:22 am    Post subject: Re: typeof() problems Reply with quote

JoeCoder wrote:
I'm having trouble with typeof(). In the simplified example below, I want methodB initialize c as a new B() and not a new A(). typeof seems to return the type of whatever's pointing to the class and not the class itself. I could use classinfo.name, but I was wondering if there's an easier way. Here's my code:
Code:
class A
{  void print()
   {  printf("A.print()\n");
   }
}

class B : A
{   void print()
   {  printf("B.print()\n");
   }
   void methodB(A a)
   {  typeof(a) c = new typeof(a);
      c.print();
   }
}

void main()
{  B b = new B();
   b.methodB(new B());
}
typeof is translated into type during compile-time, so the compiler doesnt know anything about the real passed type.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Fri Jun 23, 2006 7:19 am    Post subject: Reply with quote

The thing with typeof() is that it doesn't read quite like you might expect it to. (It took me a while to learn this, as well.) It reads as "type of this expression, if it were to be evaluated," and not "type of this value/variable" as I'd originally thought. (The specs weren't so hot when it was first introduced.) Which means, a typeof() is evaluated by traversing the parse tree of its expression and determining what the type of its result would be. This type is then substituted in place for the typeof() expression itself, and parsing continues of the parent expression.

Whee!

So, as the previous poster said, the compiler doesn't have access to information such as underlying types of instances, because nothing in the typeof() expression is actually code-generated and executed. Its only typed.

(Which, incidentally, also means any function/method calls, or other such business appearing in a typeof() means absolutely nothing. The function is never called, the assignment never implemented, etc.)
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group