473,800 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ptr to member

im trying to index through an array that is member of a class using ptr
to member

in main i tried making a ptr to the member 'arr' :

const int * testclass7::* ptrToMemberIntP tr = &testclass7::ar r;
------------------------------------------------------
the compiler cries:
cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::* '
Types pointed to are unrelated; conversion requires reinterpret_cas t,
C-style cast or function-style cast
------------------------------------------------------
I cant figure this out... isnt arr basically a const int * ?
how do i define a ptr to member arr?

class testclass7
{
public:
int arr[10];
testclass7(int arg=1)
{
for (int i=0;i<sizeof(ar r) / sizeof(*arr);i+ +)
{
arr[i] = arg+i;
}
}
};
Jul 22 '05 #1
2 1539
* trying_to_learn :
im trying to index through an array that is member of a class using ptr
to member
Instead provide a []-operator.

Pointer to member is very seldom necessary, and pointer to data member
is absolutely EVIL.

Don't use it.

in main i tried making a ptr to the member 'arr' :

const int * testclass7::* ptrToMemberIntP tr = &testclass7::ar r;
------------------------------------------------------
the compiler cries:
cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::* '
Types pointed to are unrelated; conversion requires reinterpret_cas t,
C-style cast or function-style cast
------------------------------------------------------
I cant figure this out... isnt arr basically a const int * ?
No, it's not const, and it's not a pointer: it's an array.

how do i define a ptr to member arr?

class testclass7
{
public:
int arr[10];
testclass7(int arg=1)
{
for (int i=0;i<sizeof(ar r) / sizeof(*arr);i+ +)
{
arr[i] = arg+i;
}
}
};


Like this:

int main()
{
testclass7 obj;
int (testclass7::* p)[10] = &testclass7::ar r;

for( int i = 0; i < 10; ++i )
{
std::cout << (obj.*p)[i] << std::endl;
}
}

But don't use it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
trying_to_learn wrote:
im trying to index through an array that is member of a class using ptr
to member

in main i tried making a ptr to the member 'arr' :

const int * testclass7::* ptrToMemberIntP tr = &testclass7::ar r;
------------------------------------------------------
the compiler cries:
cannot convert from 'int (testclass7::*)[10]' to 'const int *testclass7::*
' Types pointed to are unrelated; conversion requires reinterpret_cas t,
C-style cast or function-style cast
Uh... a compiler suggesting a reinterpret_cas t and a C style cast... evil.
------------------------------------------------------
I cant figure this out... isnt arr basically a const int * ?
No. arr is an array of 10 ints. Look at the error message. What you'd need
is a member pointer to an array of int, not a member pointer to a pointer.
Anyway, why do you think you need a member pointer at all?
how do i define a ptr to member arr?
Uhm, look at the error message. It tells you.
class testclass7
{
public:
int arr[10];
testclass7(int arg=1)
{
for (int i=0;i<sizeof(ar r) / sizeof(*arr);i+ +)
{
arr[i] = arg+i;
}
}
};


Jul 22 '05 #3

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

Similar topics

2
2844
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the dependency/independency on the lifecycle of the object, what is the significant differences between public member functions and public static member functions?
30
3500
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
4
2501
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
2
3640
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
8
4596
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
9
10850
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using LDIFDE as a comparison I get the same results. No members means just that, an empty group. Zero means that the DirectorySearcher.SizeLimit has been exceeded....
6
1528
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a constructor other than the default constructor. I couldn't figure out why it wasn't working so I changed the member variable to a pointer to an object instead and intialized it in the constructor and destroyed in the destructor. It bothered me that...
40
3042
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; }; how can I get a pointer to the a in an S2?
7
3816
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 {
7
3974
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member functions. Is it normal how C++ Compiler can compile large class without any problem? Didn't C++ Compiler have rules to limit the number of member functions? One big object has complex operations how member variables and member functions can be...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.