It is currently Fri Aug 18, 2017 3:01 pm




 Page 1 of 1 [ 6 posts ] 
Author Message
 Post subject: Questions about Templates
PostPosted: Mon Dec 26, 2011 2:39 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
here is a simple class and a simple global template function. question is why am I getting an access violation? The idea im i have in mind here is lets say we have a class a and instead of declaring a pointer, im trying to make a template function that accepts an arbitrary type of a class object and returns a pointer to it. What is causing this to throw an access violation error and what is the reason for it? If possible how would you be able to make this work?

The reason for this is I would prefer not to have to make a static get function that returns a this pointer for each and every class. I can just simply pass a class object in and use a pointer notation to access that objects functions. etc...

template <class T>
T* Get(T obj ) {   
   T *pt;
   return pt = &obj;
}

class ObjA {
private:
   int _A;
public:
   ObjA();
   ~ObjA();
   
   int  getA();
   void setA(int a);
};

ObjA::ObjA() {
   _A = 0;
}
ObjA::~ObjA() {
}
int ObjA::getA() {
   return _A;
}
void ObjA::setA( int a ) {
   _A = a;
}

int main() {

   ObjA a;
   Get( a )->setA( 33 );
   
   printf( "\n%d \n", a.getA() );


   return 0;
}


Offline
 Profile  
 
 Post subject: Re: C++ VMK 14 - Templates
PostPosted: Mon Dec 26, 2011 6:44 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
The problem here is that you are trying to access a variable that is out of scope and therefore no longer available.

If we look at the Get function call we see that you create a variable called pt on the stack and then you return it. This variable is then copied to your main function when it is returned BUT it doesn't have a place to live in the main function. Therefore as soon as you execute the next statement being the setA function you are using a pointer that no longer exists.

To fix this you can modify the main function to look like this instead:
   ObjA a;
   ObjA* p = Get( a );
   p->setA( 33 );


By introducing p you can save the copy of the returned variable from the Get function thus using it on the next line will be no problem (so long as the object 'a' is still in scope of course).

I'm not sure how you plan on using this Get function because I don't see much benefit to it. You could replace your code with the following and it would do basically the same thing.

   ObjA a;   
   ObjA* p = &a;
   p->setA( 33 );


Offline
 Profile  
 
 Post subject: Re: C++ VMK 14 - Templates
PostPosted: Mon Dec 26, 2011 5:45 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
Ok, I see what you are saying. The idea i had in mind is as such:

class Car;
class House;
class Person;
class Weapon;

etc... and in the source i had in mind that instead of creating a pointer for each object.

Car *pCar;
House *pHouse;
Person *pPerson;
Weapon pWeapon;

create a global function called Get() which is a template function that would except any type class object and would itself create the pointer to this class and return it. The idea was to call this class inside of main or any other class or function, were a local object is already declared.

Car car;
// The function would be used where passing in car is a valid type through the template features and
to return its pointer to access its members through the pointer -> instead of the . from the local object.

Get( car )->getEnginesPerformanceRating();
Since I have the function Get() returning a pointer to the class object car "even though it has gone out of scope" i was working mainly with the concept of templates, or practicing using them. I am just trying to get a better feel for templates and how to implement them properly more than the actual use of this get function.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 14 - Templates
PostPosted: Tue Mar 06, 2012 3:41 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I am stumped on this one....

template< class T >
class Data {
public:
   T _value;

public:
   Data( T value );
   ~Data();

};

template< class T >
Data<T>::Data( T value ) {
   _value = value;
} // Data

template< class T >
Data<T>::~Data() {
} // ~Data

Above code in some class file..

Inside of main.
#include "*.h"

int _tmain ( int argc, _TCHAR* argv[] ) {
   int i = 3;
   Data<int> d(i);

   printf( "%d", d._value );

   return 0;
}


A simple templated class with one param. which the param type is passed into the constructor and saved into member variable. This *.cpp and main.cpp both compile but it does not build...

Eventually my goal here is to have on the lines of this structure...

A templated base class with one param T. class will have a pure virtual inline constructor, a virtual destructor. The T param passed into the constructor will be saved to a protected member. The class has a public enumeration and a private member variable of this enum.

Derived classes so far 2 of them are templated classes with one param T. Each are publicly inherited from base class. They have an inline constructor and a destructor. The variables passed into their constructor are passed back to the base class. One through the base class initilizer constructor and the other within the constructor saved directly into the protected member. A wrapper class that either contains both of the derived classes or inherits from both derived classes.

An object declaration would be something like this..

WrapperClass w<dataType>( value, enum type );
where the value gets saved into the base class, and the enum type determines which derived class is used. The base class only contains the value saved and which type of derived class should be used.
The Derived classes will contain all the methods or functions. The base class may however contain one public method which would return a pointer to either of the derived classes depending on its member variable. To set the private member variable in the base class I am not using set or get functions I am passing directly into the constructor from the derived classes' constructor/s. This is the over all approach I am aiming for however If I can not get the above simple code to work, then this description here so far will not work. I need a working base to go off of. Any ideas would be great. Oh in the end I would also like to be able to possibly typedef the classes.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 14 - Templates
PostPosted: Fri Mar 09, 2012 5:45 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
I had a look at your sample code and I don't see anything wrong with it. I took the code and successfully built and ran the code.

So what is the error that you are seeing?

The only thing that I can think of that you may have done incorrectly is put the implementation of the class template in the cpp file rather than the header file. If you do this then you'll get a linker error because the compiler is not able to resolve your template implementation.

The template implementation must go into the header file not the cpp file.


Offline
 Profile  
 
 Post subject: Re: Questions about Templates
PostPosted: Fri Mar 09, 2012 12:09 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
Lol, that was exactly the case. I had figured it out from reading other forums. But thank you for the reply.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 6 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


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

Jump to:  

cron