Solved

Class wrapper

Posted on 2002-03-19
5
300 Views
Last Modified: 2010-04-02
Hi, experts!


I have to adapt some product to new operating environment. The product consists of machine independent core (a lot of code) and machine dependent layer.
The only problem I have adapting the core layer, that the size of stack on the new machine is very small. There are several large classes, and lots of functions that use them as local variables, so in many cases nested calls will cause stack overflow.

My main idea is to create some wrapper class (like smart ptr) that will replace a real class. When it used locally, wrapper will allocate memory from the heap, instead of stack, and delegate method calls to the real class.
Something like this:
CHugeClass
{
     int f();
}
I rename it to
CRawHugeClass
Than create a new
CHugeClass
{
     CHugeClass() {m_p = new CrawHugeClass;}
     int f() {m_p->f();}
     //here I have a problem with class members!!!
}

So I need to change several class definitions instead of many KLines :) of code;

I am looking for an idea how to deal with class members of CHugeClass in this case,
or any better idea at all.

Thank you in advance.
0
Comment
Question by:jemax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6880828
What about making the wrapper a 'friend' of the original class? That should actually work...
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6880895
If you have a lot of inter-dependent classes this method could turn out to be a nightmare and also cause headaces with maintenance later. If you have only a few class (10 or less,  I would guess) then it's OK

More than that I would rather go the long route and go through the many KLines of code and do it the "proper" way. This way is much more modular anyway. You change one function, see if it works, move on to the next etc.

You should be able to write a little "conversion" utility without too much hassle.
0
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 200 total points
ID: 6881133
Idea : Using reference

Example :

#include <memory>
using namespace std;

class Foo
{
public:

    void f() {};
    void g() {};

private:
    int arr[2000];
};


void main(int argc, char **argv)
{
    // using reference
    auto_ptr<Foo> sp( new Foo )
    Foo & obj = *sp;
    obj.f();
    obj.g();
   



    // Short cut
    Foo & foo = *auto_ptr<Foo>( new Foo );
    foo.f();
    foo.g();

}

0
 
LVL 1

Author Comment

by:jemax
ID: 6884269
Thank you for your comments.

jkr:
it is not an issue of members visibility. Operator '.' in CWrapper should select a member from CReal.

LoungeLizard:
There is a dozen of large classes. I agree, going over the code is the best way, but it must be done in short time.
Utility...
C c, *pc; //keep all decl. in mind for correct replacement
C& rc1 = *pc, rc1 = c;
c. , &c , f(C* pc), f(C c)
et_c :)
I am afraid it will not be small, but "fully functional":) c++ parser

thienpnguyen:
It is great idea, but I still have to change all the functions, because of "= *sp;" :(
Anyway, it is the best comment. If there is no greater solution in a couple of days, the points are yours.

Thanks.
   


0
 
LVL 1

Author Comment

by:jemax
ID: 6884283
Thank you for your comments.

jkr:
it is not an issue of members visibility. Operator '.' in CWrapper should select a member from CReal.

LoungeLizard:
There is a dozen of large classes. I agree, going over the code is the best way, but it must be done in short time.
Utility...
C c, *pc; //keep all decl. in mind for correct replacement
C& rc1 = *pc, rc1 = c;
c. , &c , f(C* pc), f(C c)
et_c :)
I am afraid it will not be small, but "fully functional":) c++ parser

thienpnguyen:
It is great idea, but I still have to change all the functions, because of "= *sp;" :(
Anyway, it is the best comment. If there is no greater solution in a couple of days, the points are yours.

Thanks.
   


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 135
Socket Programming (Unix) 8 153
Unresolved External Symbols 3 130
Create a path if not exists 7 108
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question