473,385 Members | 2,180 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,385 software developers and data experts.

Member offset pointers ?

Hi,

In C++ you could define a member offset pointer which worked on any instance
of a class.

i.e.
class MyClass
{
int IntOne;
int IntTwo;

void SetInt(int MyClass::* p)
{
this->*p = 100;
}
}

void Eg()
{
MyClass c;

// Sets c.IntOne
c.SetInt(&MyClass::IntOne);
// Sets c.IntTwo
c.SetInt(&MyClass::IntTwo);
}

A rather simplistic illustration, and my apologies if the syntax isn't
exactly right - I haven't worked with C++ for a couple of years. Is there
anything like this in C# ? I've searched and can't find it.

Thanks,

Adam.

--

- Adam.

==============================
Ad*********@Omnibus.NOSPAMMYSPAMSPAM.co.uk
Sep 22 '06 #1
1 1881
you can pass things by reference using the ref keyword. This enables you to
pass a member variable to a function and have that function update the member
variable.
e.g

void SetInt(ref int p)
{
p = 100;
}
HTH
Ciaran O'Donnell

"Adam Benson" wrote:
Hi,

In C++ you could define a member offset pointer which worked on any instance
of a class.

i.e.
class MyClass
{
int IntOne;
int IntTwo;

void SetInt(int MyClass::* p)
{
this->*p = 100;
}
}

void Eg()
{
MyClass c;

// Sets c.IntOne
c.SetInt(&MyClass::IntOne);
// Sets c.IntTwo
c.SetInt(&MyClass::IntTwo);
}

A rather simplistic illustration, and my apologies if the syntax isn't
exactly right - I haven't worked with C++ for a couple of years. Is there
anything like this in C# ? I've searched and can't find it.

Thanks,

Adam.

--

- Adam.

==============================
Ad*********@Omnibus.NOSPAMMYSPAMSPAM.co.uk
Sep 22 '06 #2

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
4
by: Dave | last post by:
Hello all, For purposes of understanding the language better, I tried to determine the offset of a struct member by using pointers to members (instead of something such as the offsetof macro). ...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
11
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with...
40
by: Steve Rencontre | last post by:
I can't for the life of me see how to do pointer-to-member when the member is actually part of an embedded structure. That is, if I have: struct S1 { int a; }; struct S2 { S1 s; int b; }; ...
9
by: removeps-generic | last post by:
I have struct X { double array; }; I want to form a pointer to the 5th element of X::array. The type of the pointer should be "double X::*" or "double* X::*" or something along those...
8
by: josh | last post by:
Hi I don't well understand if I have class Test { public: Test(int i){ val=i;} int double_val() { return val+val;} int val; };
2
by: Imre | last post by:
Hi I know that offsetof is basically a C leftover, and only works for POD types, not classes, so it is recommended that I use pointers to members instead. However, I have a problem where I don't...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.