473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

address of class from within class method

Is there a way to get a pointer to a class from inside one of the
class's methods?

Jun 2 '06 #1
12 2859
pe****************@hotmail.com wrote:
Is there a way to get a pointer to a class from inside one of the
class's methods?


Every class can refer to itself by a "this" pointer. Look it up in your
favorite C++ book.

Cheers! --M

Jun 2 '06 #2
pe****************@hotmail.com wrote:
Is there a way to get a pointer to a class from inside one of the
class's methods?


What do you mean by a "pointer to a class"? A class is not an object,
it is a type. You can get a pointer to the current object by using the
keyword "this".
Jun 2 '06 #3
mlimber wrote:
pe****************@hotmail.com wrote:
Is there a way to get a pointer to a class from inside one of the
class's methods?


Every class can refer to itself by a "this" pointer. Look it up in your
favorite C++ book.


No, every *object* can refer to itself by a "this" pointer. A class is
not an object, and therefore doesn't have an address, which a pointer
can point to.

Jun 2 '06 #4
red floyd wrote:
pe****************@hotmail.com wrote:
Is there a way to get a pointer to a class from inside one of the
class's methods?


What do you mean by a "pointer to a class"? A class is not an object,
it is a type. You can get a pointer to the current object by using the
keyword "this".

Foot note: A class is a type, see 3.9.2/1
Jun 2 '06 #5

red floyd wrote:
What do you mean by a "pointer to a class"? A class is not an object,
it is a type. You can get a pointer to the current object by using the
keyword "this".


I meant an instance of a class.

Will the keyword "this" give me the address of the instance of the
class that called the class method which is using the keyword "this"?

Jun 2 '06 #6
On Fri, 02 Jun 2006 12:56:33 -0700, peregrine_falcon12 wrote:
Is there a way to get a pointer to a class from inside one of the class's
methods?


This may sound like nitpicking... but a class doesn't have an address. A
class merely describes what an object will "look like". _Instances_ of a
class have addresses.

So, could you restate your question?
Jun 2 '06 #7

Andre Kostur wrote:
On Fri, 02 Jun 2006 12:56:33 -0700, peregrine_falcon12 wrote:
Is there a way to get a pointer to a class from inside one of the class's
methods?


This may sound like nitpicking... but a class doesn't have an address. A
class merely describes what an object will "look like". _Instances_ of a
class have addresses.

So, could you restate your question?


I meant an instance of a class.

Jun 2 '06 #8

pe****************@hotmail.com wrote:
red floyd wrote:
What do you mean by a "pointer to a class"? A class is not an object,
it is a type. You can get a pointer to the current object by using the
keyword "this".


I meant an instance of a class.

Will the keyword "this" give me the address of the instance of the
class that called the class method which is using the keyword "this"?


no.

Jun 2 '06 #9
pe****************@hotmail.com wrote:
Will the keyword "this" give me the address of the instance of the
class that called the class method which is using the keyword "this"?


Your phrase "the instance of the class that called the class method" is
not clear. Consider this:

struct A;

void Baz( A* ) {}

struct A
{
void Foo() { Baz( this ); } // Ok
};

struct B
{
void Bar( A& a )
{
a.Foo(); // Ok
Baz( this ); // Error
}
};

int main()
{
A a;
B b;
b.Bar( a );
}

In this example, the class instance that calls A::Foo() is an instance
of B. Within A::Foo(), "this" refers to an A object. Within B::Bar(),
"this" points to a B object, which is why the call to Baz generates an
error in B::Bar().

Cheers! --M

Jun 2 '06 #10
red floyd wrote:
mlimber wrote:
pe****************@hotmail.com wrote:
Is there a way to get a pointer to a class from inside one of the
class's methods?


Every class can refer to itself by a "this" pointer. Look it up in your
favorite C++ book.


No, every *object* can refer to itself by a "this" pointer. A class is
not an object, and therefore doesn't have an address, which a pointer
can point to.


Sorry. I got typing too quickly.

Cheers! --M

Jun 2 '06 #11
Problem Solved!!!

Thanx mlimber!

Jun 2 '06 #12
On Fri, 02 Jun 2006 13:30:21 -0700, peregrine_falcon12 wrote:
Andre Kostur wrote:
On Fri, 02 Jun 2006 12:56:33 -0700, peregrine_falcon12 wrote:
> Is there a way to get a pointer to a class from inside one of the
> class's methods?


This may sound like nitpicking... but a class doesn't have an address.
A class merely describes what an object will "look like". _Instances_
of a class have addresses.

So, could you restate your question?


I meant an instance of a class.


OK, from inside any of your methods (except static methods), they all have
a pointer called "this" which points to the instance on which this method
was invoked.
Jun 2 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
0
by: landagen | last post by:
I am using the MethodRental class, but i noticed that you have to pass a byte for to replace the body of a method. The problem comes when i want to access a field within the class where I am...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
4
by: toton | last post by:
Hi, How to find address of a class from inside? My class having a overloaded new & delete. new allocates the memory like void* operator new(std::size_t size) throw(){ cout<<"op new...
8
by: gilad | last post by:
hi, I would like to get the address of a class method in an instance. In the following sample the third like access the method. how can I get the address into a long variable - something like -...
1
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.