Author Topic: typedef to class?  (Read 20692 times)

Offline blueshake

  • Regular
  • ***
  • Posts: 459
typedef to class?
« on: August 12, 2009, 02:56:11 am »
hello:
      in this thread ,http://forums.codeblocks.org/index.php/topic,10641.0.html
the typedef problem can not be solved.
codes like this:
Code
#include <iostream>

using namespace std;

class Pt
{
public:
    int x;
    int y;
};
typedef Pt subpt;
typedef subpt sub;
int main()
{
    sub pp;
    pp. // not work here.
    cout << "Hello world!" << endl;
    return 0;
}

pp get nothing.
I think can use this patch.
but I not sure it is right or not to do this.

here is the patch.

Code
Index: parserthread.cpp
===================================================================
--- parserthread.cpp (revision 5730)
+++ parserthread.cpp (working copy)


@@ -1707,15 +1737,60 @@
 #if PARSER_DEBUG_OUTPUT
     Manager::Get()->GetLogManager()->DebugLog(F(_("Adding typedef: name='%s', ancestor='%s'"), components.front().c_str(), ancestor.c_str()));
 #endif
-    Token* tdef = DoAddToken(tkTypedef, components.front(), lineNr, 0, 0, args);

+    Token* tdef = DoAddToken(tkClass, components.front(), lineNr, 0, 0, args);

and pp has the tip now.see the attachment.

any comment???
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5915
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: typedef to class?
« Reply #1 on: August 12, 2009, 03:39:54 am »
Your attachment is 0kb? :(
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: typedef to class?
« Reply #2 on: August 12, 2009, 06:21:18 am »
sorry.I just upload the pic,see the attachment.

[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: typedef to class?
« Reply #3 on: August 12, 2009, 10:46:48 am »
any comment???
..does it still work if you make a type out of a struct using a typedef?
Meaning: Does the struct and the typedef still code-complete?
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: typedef to class?
« Reply #4 on: August 12, 2009, 12:07:55 pm »
yes,i think so.
here is the test codes.
Code
typedef unsigned int unlong;
typedef class cls
{
    int xx;
    int yy;
}clssub;
int main()
{
    sub pp;
    unlon
    cout << "Hello world!" << endl;
    return 0;
}

the unlong can be recognized correctly.
see the attachment.
my confuse is if use this ,it seems that the tkTypedef  will have no use any more.

[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: typedef to class?
« Reply #5 on: August 12, 2009, 12:13:52 pm »
yes,i think so.
What I meant was something like this, actually:
Code
struct tStruct
{
  SomeClass* some_class;
  int some_int;
  void* some_void;
};
typedef struct tStruct tStruct;
...or even:
Code
struct sStruct
{
  SomeClass* some_class;
  int some_int;
  void* some_void;
};
typedef struct sStruct tStruct;
(Notice the different name of the struct).
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: typedef to class?
« Reply #6 on: August 12, 2009, 12:43:26 pm »
I test the codes in cc-branch version without applying the patch.
Code
class sStruct
{
  SomeClass* some_class;
  int some_int;
  void* some_void;
};
typedef class sStruct tStruct;

int main()
{

    cout << "Hello world!" << endl;
    return 0;
}
it seems that the codes can not be parsed correctly.

here tStruct is recognized as a variable.
and the main is a typedef. :(
does it exist or something .
if I apply the patch , seems nothing changed :(
i will check it if i am free

[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: typedef to class?
« Reply #7 on: August 12, 2009, 01:12:37 pm »
I test the codes in cc-branch version without applying the patch.
it seems that the codes can not be parsed correctly.
Oops - that makes me wonder. Because the used to work...?!
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5915
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: typedef to class?
« Reply #8 on: August 12, 2009, 01:24:52 pm »
I'm not quite understand you guys are taking about.
For me, I think blueshake's comment is right:

Quote
my confuse is if use this ,it seems that the tkTypedef  will have no use any more.

We can just regard the "typedef" as a public derived class of the original type.
Something like this:
Code
class sStruct
{
  SomeClass* some_class;
  int some_int;
  void* some_void;
};
typedef class sStruct tStruct;

int main()
{

    cout << "Hello world!" << endl;
    return 0;
}


We can consider tStruct is a derived class from "sStruct".
That's all my want to say.


If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: typedef to class?
« Reply #9 on: August 12, 2009, 02:10:20 pm »
Quote
Oops - that makes me wonder. Because the used to work...?!
sorry! I don't know it doesn't work before.

@ollydbg
what we are talking about is codes like these can not be parsed correctly.
Code
typedef class sStruct tStruct;

even i use the patch .nothing changed.

by the way . what does it mean of "Oops".
« Last Edit: August 12, 2009, 02:12:20 pm by blueshake »
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: typedef to class?
« Reply #10 on: August 12, 2009, 05:27:44 pm »
Quote
Oops - that makes me wonder. Because the used to work...?!
sorry! I don't know it doesn't work before.
What I meant is that we did several modifications to CC that such constructs were parsed correctly (~1 year back). And in fact it worked! som something we applied in the meantime broke this feature (again).

[/quote]
by the way . what does it mean of "Oops".
Nothing, really. It's a word you shout out if something happens you don't expect.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: typedef to class?
« Reply #11 on: August 15, 2009, 03:10:21 pm »
@MortenMacFly

i think the cc can use the followed patch to make codes below parsed correctly.
Code
typedef class sStruct tStruct;

Code
Index: parserthread.cpp
===================================================================
--- parserthread.cpp (revision 5731)
+++ parserthread.cpp (working copy)
@@ -1249,6 +1337,63 @@              
                
                struct HiddenStruct yy;
               */
+                if (m_ParsingTypedef)
+                {
+         m_Tokenizer.UngetToken();
+                     break;
+                }
                if (TokenExists(current, m_pLastParent, tkClass))
               {
                    if (!TokenExists(next, m_pLastParent, tkVariable) )
Code
+    Token* tdef = DoAddToken(tkClass, components.front(), lineNr, 0, 0, args);
-       //Token* tdef = DoAddToken(tkTypedef, components.front(), lineNr, 0, 0, args);
« Last Edit: August 15, 2009, 03:13:10 pm by blueshake »
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: typedef to class?
« Reply #12 on: August 15, 2009, 03:15:59 pm »
see the attachment,

[attachment deleted by admin]
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?