LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2004, 05:09 PM   #1
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
c++, casting, templates


i thought i knew c++ better than this but i obviously dont. basically, i want to know what function i could add to MMObjectPtr that replaces the functionallity of cast() so that instead of b=i.cast(); i can say b=i; anyway heres the code, i know ive got a memory leak but this is just demonstational code.

<edit>
never mind, ive just found the syntax for the casting operator, allows you to cast any type into another

operator T*() {return ptr;}

replaces cast() very nicely
</edit>

Code:
#include <iostream>

using namespace std;

class MMObject {
private:
	int count;
protected:
	MMObject() : count(0) {}
public:
	void AddRef() {	count++;}
	void ReleaseRef() {count--;}
};

template<class T>class MMObjectPtr
{
private:
	T *ptr;
public:
	MMObjectPtr() : ptr(NULL) {}
	~MMObjectPtr() { if(ptr != NULL) ptr->ReleaseRef(); }
	T *cast() const { return ptr; }
	MMObjectPtr(T *p) { *this=p; }
	MMObjectPtr<T> &operator=(T *p) {
		if(ptr !=NULL) ptr->ReleaseRef();
		ptr=p;
		if(ptr !=NULL) ptr->AddRef();
		return *this;
	}
	MMObjectPtr<T> &operator=(MMObjectPtr<T> &p) {*this=p.ptr;}
	T *operator->() const {	return ptr; }
};

class base : public MMObject
{
public:
	virtual void a()=0;
};

class imp : public base
{
public:
	void a() {cout << "Youve called imp::a()" << endl;}
};

int main()
{
	MMObjectPtr<base> b;
	MMObjectPtr<imp> i;

	i=new imp;
	b=i.cast();
	b->a();

	return 0;
}

Last edited by kev82; 08-24-2004 at 05:34 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A Couple of C++ questions, casting -0- Programming 7 06-27-2005 12:14 PM
casting question in kernel source code linetnew Programming 3 04-18-2005 04:55 AM
How to do type casting in Sparc Assembly Language foxele Programming 1 10-09-2004 04:40 PM
casting cynthia Programming 6 10-05-2004 03:58 AM
C++ Casting bretthoward Programming 1 05-11-2004 01:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration