473,881 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make it really constant?

Dear All,

I have a question. Assume

struct A
{
int *p, *q;
};

struct B
{
A a;

const struct& GetA() const
{
return a;
}
};

I hope B::GetA() cannot change p, q pointed values, but it can, such
as

*(B::GetA().p) = 3;

How can I make it really constant? One way may be defining p and q as
follows
const int * p;
const int * q;

Whenever p, q pointed values want to change in class/struct A, do the
const_cast. But this is too cumbersome.

I remember a constant std::vector cannot change its values, even
thought it is also implemented by a inner pointer. How does
std::vector make the values constant?

Thanks and best regards,

Shuisheng
Sep 19 '08 #1
5 1601
On Sep 19, 3:12 pm, shuisheng <shuishen...@ya hoo.comwrote:
Dear All,

I have a question. Assume

struct A
{
int *p, *q;

};

struct B
{
A a;

const struct& GetA() const
{
return a;
}

};

I hope B::GetA() cannot change p, q pointed values, but it can, such
as

*(B::GetA().p) = 3;

How can I make it really constant? One way may be defining p and q as
follows
const int * p;
const int * q;

Whenever p, q pointed values want to change in class/struct A, do the
const_cast. But this is too cumbersome.
If you want this kind of control, you should make A a class, with p
and q private. Then provide const A& get_a() const and A& get_a()
members etc..

Tony
Sep 19 '08 #2
to***********@y ahoo.co.uk wrote:
On Sep 19, 3:12 pm, shuisheng <shuishen...@ya hoo.comwrote:
>Dear All,

I have a question. Assume

struct A
{
int *p, *q;

};

struct B
{
A a;

const struct& GetA() const
{
return a;
}

};

I hope B::GetA() cannot change p, q pointed values, but it can, such
as

*(B::GetA(). p) = 3;

How can I make it really constant? One way may be defining p and q as
follows
const int * p;
const int * q;

Whenever p, q pointed values want to change in class/struct A, do the
const_cast. But this is too cumbersome.

If you want this kind of control, you should make A a class, with p
and q private. Then provide const A& get_a() const and A& get_a()
members etc..
With this solution you can still change values. In the
const A& get_a() const
method, you couldn't change pointers, but could still change the value
they point to.

Only way is to change struct A
Sep 19 '08 #3

shuisheng wrote:
Dear All,

I have a question. Assume

struct A
{
int *p, *q;
};
add an assist method in you struct A.

i don't quite catch your program's requires, but you can try something
like this anyway.

cheers & hth,
Jim

//=============== =============== =============== =======
#include <iostream>

using namespace std;

struct A
{
int a;
int* pa;
const int* const get_pa() const
{
return pa;
}
};

struct B
{
A a;

const A& getA() const
{
return a;
}
};

int main()
{
B b;
int i = 4;
// b.getA().a = 3;
*(b.getA().get_ pa()) = i; // fail
*(b.getA().pa) = i; // OK
return 0;
}
Sep 19 '08 #4
On Sep 19, 3:12 pm, shuisheng <shuishen...@ya hoo.comwrote:
struct A { int *p, *q; };

struct B
{
A a;
const struct& GetA() const { return a; }
};

I hope B::GetA() cannot change p, q pointed values, but it can, such
as

*(B::GetA().p) = 3;
tony_in_da...@y ahoo.co.uk wrote:
If you want this kind of control, you should make A a class, with p
and q private. Then provide const A& get_a() const and A& get_a()
members etc..
On Sep 19, 4:31 pm, anon <a...@no.invali dwrote:
With this solution you can still change values. In the
const A& get_a() const
method, you couldn't change pointers, but could still change the value
they point to.

Only way is to change struct A [snip]
"If you want this kind of control, you should make A a class, with p
and q private."...

struct A
{
private:
int *p, *q;
};

"Then provide const A& get_a() const and A& get_a() members etc.."...

struct B
{
A a;
const struct& GetA() const
{
return a;
}

struct& GetA()
{
return a;
}
};

Clearly, there's nothing public to access in a. By implication, you
must expose p and q similarly...

struct A
{
const int* get_p() const { return p; }
int* get_p() { return p; }

const int* get_q() const { return q; }
int* get_q() { return q; }

private:
int *p, *q;
};

And there you have it.

Tony
Sep 19 '08 #5
In article
<8f************ *************** *******@d1g2000 hsg.googlegroup s.com>,
shuisheng <sh*********@ya hoo.comwrote:
struct A
{
int *p, *q;
};

struct B
{
A a;

const struct& GetA() const
{
return a;
}
};

I hope B::GetA() cannot change p, q pointed values, but it can, such
as

*(B::GetA().p) = 3;

How can I make it really constant? One way may be defining p and q as
follows
const int * p;
const int * q;

Whenever p, q pointed values want to change in class/struct A, do the
const_cast. But this is too cumbersome.

I remember a constant std::vector cannot change its values, even
thought it is also implemented by a inner pointer. How does
std::vector make the values constant?
You could write a smart pointer helper class that propagates const one
level down:

template<typena me T>
class Ptr {
T* p;
public:
Ptr& operator = ( T* rhs ) { p = rhs; return *this; }

T & operator * () { return *p; }
T const& operator * () const { return *p; }

T * operator -() { return p; }
T const* operator -() const { return p; }
// ...
};

struct A { Ptr<intx; };

int f( A& a )
{
a.x = new int; // OK
*a.x = 1234; // OK
return *a.x; // OK
}

int fc( A const& a )
{
a.x = new int; // error
*a.x = 1234; // error
return *a.x; // OK
}
Sep 19 '08 #6

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

Similar topics

11
2388
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
11
7801
by: Bill Nguyen | last post by:
I need to make a set of constants to be available for the whole application. Public const is confined to the form from which constants are declared. Is there a way to declare once and all forms can access the constant values? Thanks Bill
14
1860
by: KK | last post by:
/* Target - read an integer from a binary file */ unsigned int Byte2Int(char *buff) { unsigned char* byte = reinterpret_cast<unsigned char*> (buff); return ((byte<<24)|(byte<<16)|(byte<<8)|(byte)); } /* part of main funciton */ ifstream fp("in.bin",ios::binary); char buff;
7
3328
by: aviad | last post by:
I am writing a Form application I need it to fit both resolution of 1600*1200 and 800*600 (and any other resolution that might jump in) the application is meant for regular PCs another question is what the difference between the Dock and Anchor properties? i tried to use them (mutual exclusive of course) but with no success
8
1435
by: padew | last post by:
I want make a vfariabl on the fly so: for (var i = 0; i<2; i++) { z+i=new Array("a"); fo to have: z0=a z1=a z1=a
1
14844
by: Peter | last post by:
Hi, I know that it is possible to build up a variable name based on a string and another variable name i.e. $varnum=5; ${"varnumber_$varnum"} = 'This variable should be called varnumber_5'; but is it possible to do this for a constant? For example I may have constants set up as
7
1637
by: John Harrison | last post by:
This is from SGI's FAQ, its the justification for why list<T>::size() is linear time in their library (and in gcc library too since their code is based on SGI) <quote> Why is list<>::size() linear time? The size() member function, for list and slist, takes time proportional to the number of elements in the list. This was a deliberate tradeoff. The only way to get a constant-time size() for linked lists would be to
4
3091
by: jaime | last post by:
Hi again all. Given the line: const int x=5; Can I then use "x" as a constant expression? (By "constant expression", I mean "constant expression" as defined in the C99 standard) I've been searching google for 2 days now trying to answer this myself, and I'm just getting more and more confused (some things I read make me think "yes", while some things I read make me think "no").
12
2495
by: raylopez99 | last post by:
I have an array that I wish to preserve as "read only". The array holds references to variables myObjects instantiated by new that change all the time. The variables are part of a class that I used ICloneable on, namely "Clone();" (deep and/or shallow copies worked the same for this particular class). Using ICloneable, I am able to successfully make a copy of the variables like so:
0
9926
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9775
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
10715
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10396
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
9550
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
7952
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...
0
5974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
4194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.