Solved

Class wrapper

Posted on 2002-03-19
5
295 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
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now