Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 'Extract Method' fails to detect needed parameters
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nodet
New Member

8 Posts

Posted - Mar 08 2007 :  08:52:22 AM  Show Profile  Reply with Quote
Hi,

I am using build 1548 of Visual Assist X. I noticed a problem when extracting some code that uses pointers to members: the method that is created misses some parameters... Consider the following code:

class Elem;
class K {
  int getNumberOfElements() const;
  Elem* getElement(int) const;  
  void method();
};

void K::method() {
  int (K:: *getNb)() const;
  getNb = &K::getNumberOfElements;
  Elem* (K:: * get)(int) const;
  get = &K::getElement;

  for (int i = 0; i < (this->*getNb)(); ++i) {
    Elem* elem = (this->*get)(i);
    // Do something with elem
  }
}

Select the whole 'for' loop (the blue lines), and choose 'Extract Method'. You get the following incorrect code:

class Elem;
class K {
  int getNumberOfElements() const;
  Elem* getElement(int) const;  
  void method();

  void MyMethod() {
    for (int i = 0; i < (this->*getNb)(); ++i) {
      Elem* elem = (this->*get)(i);
      // Do something with elem
    }
  }
};

void K::method() {
  int (K:: *getNb)() const;
  getNb = &K::getNumberOfElements;
  Elem* (K:: * get)(int) const;
  get = &K::getElement;

  MyMethod();
}

instead of

class Elem;
class K {
  int getNumberOfElements() const;
  Elem* getElement(int) const;  
  void method();

  void MyMethod(int (K:: *getNb)() const, Elem* (K:: * get)(int) const) {
    for (int i = 0; i < (this->*getNb)(); ++i) {
      Elem* elem = (this->*get)(i);
      // Do something with elem
    }
  }
};

void K::method() {
  int (K:: *getNb)() const;
  getNb = &K::getNumberOfElements;
  Elem* (K:: * get)(int) const;
  get = &K::getElement;

  MyMethod(getNb, get);
}

Not a big deal (I don't write such code every day...), but I thought you'd like to know...
Thanks.

Xavier Nodet
"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety." - Benjamin Franklin, 1759.

feline
Whole Tomato Software

United Kingdom
18952 Posts

Posted - Mar 08 2007 :  11:41:14 AM  Show Profile  Reply with Quote
I suspect you have missed a bit when you constructed this example. In the K::method function the first line is:

	int (K:: *getNb)() const;


where VA is underlining "getNb" as a mistyped symbol, which is reasonable, since this code does not compile, I just checked in VS2005. Since VA does not understand "getNb" it makes sense that Extract Method is going to have problems with this code as well.

"getNb" seems to be a function pointer, but why are you trying to create a function pointer variable as a class member inside a member function? I am feeling a bit confused by this

zen is the art of being at one with the two'ness
Go to Top of Page

nodet
New Member

8 Posts

Posted - Mar 08 2007 :  4:31:46 PM  Show Profile  Reply with Quote
I don't understand! The code does compile for me:

W:\\MSVC8\\TestScheduler>type testVAX.cpp
//
// Test Visual Assist 'ExtractMethod'
//

class Elem;
class K {
  int getNumberOfElements() const;
  Elem* getElement(int) const;
  void method();

};

void K::method() {
  int (K:: *getNb)() const;
  getNb = &K::getNumberOfElements;
  Elem* (K:: * get)(int) const;
  get = &K::getElement;

  for (int i = 0; i < (this->*getNb)(); ++i) {
    Elem* elem = (this->*get)(i);
    // Do something with elem
  }
}

W:\\MSVC8\\TestScheduler>cl /c testVAX.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

testVAX.cpp

W:\\MSVC8\\TestScheduler>

I checked that it also compiles with MSVC 7 (13.10.3077) and gcc 3.4.4.

Of course, this code looks rather bizarre, but this is a trimmed down version of a more complicated code, where the method that I extract is called several times with different sets of parameters.

Xavier Nodet
"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety." - Benjamin Franklin, 1759.
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18952 Posts

Posted - Mar 09 2007 :  12:41:44 PM  Show Profile  Reply with Quote
Apologies, my mistake. The compilation errors were unresolved externals, I should have checked the nature of the errors more carefully. Once I added dummy implementations in the class K the errors went away.

I have put in a bug report for VA not understanding the function pointer variables, since this is a problem, and I suspect is the real cause of the bug with Extract Method:

case=5433

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000