473,778 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Const iterator, private struct.

Hi,

Sorry, not very clear from the title but wasn't sure what to call it.
I was experimenting with the following in GCC 4.0.1 and didn't achieve
what I expected. I was trying to keep the internal class private, to
prevent creation, but also in the hope it might achieve something like
when you use "class <foo>;" to early declare to allow pointer passing,
but cannot access contents.

Also I passed out a const iterator containing this object expecting it
to make what it was iterating over const, but gcc allows me to change
it.

Just wondered what I should actually expect (found behaviour was
correct?) and anything close (if possible) to roughly achieve what I
was expecting.

#include <map>
#include <string>

class A
{
private:
struct Test
{
int a;
int b;
};
typedef std::map<std::s tring,Test *Test2;

public:
typedef const Test2::const_it erator Test3;

private:
Test2 m_test;

public:
Test3 foo () { return m_test.end (); }
};

int main ()
{
A a;
A::Test3 test = a.foo ();
// Accesses object of private type...
// Didn't want, hoping for compiler error
int b = (*test).second->a;
(*test).second->a = 1; // modified const...
return 0;
}

Regards,
Simon

Aug 14 '06 #1
1 1875
swhite wrote:
Hi,

Sorry, not very clear from the title but wasn't sure what to call it.
I was experimenting with the following in GCC 4.0.1 and didn't achieve
what I expected. I was trying to keep the internal class private, to
prevent creation, but also in the hope it might achieve something like
when you use "class <foo>;" to early declare to allow pointer passing,
but cannot access contents.

Also I passed out a const iterator containing this object expecting it
to make what it was iterating over const, but gcc allows me to change
it.

Just wondered what I should actually expect (found behaviour was
correct?) and anything close (if possible) to roughly achieve what I
was expecting.

#include <map>
#include <string>

class A
{
private:
struct Test
{
int a;
int b;
};
typedef std::map<std::s tring,Test *Test2;

public:
typedef const Test2::const_it erator Test3;

private:
Test2 m_test;

public:
Test3 foo () { return m_test.end (); }
};

int main ()
{
A a;
A::Test3 test = a.foo ();
// Accesses object of private type...
// Didn't want, hoping for compiler error
int b = (*test).second->a;
(*test).second->a = 1; // modified const...
return 0;
}

Regards,
Simon
Well, the reason is const_iterator does not allow you to modify the
structure of the container, but it says nothing with regards to the
object themselves. To achieve what you want you need to do that:

#include <string>
#include <map>

class A
{
public:
struct Test
{
int a;
int b;
};
typedef std::map<std::s tring,const Test *Test2;
typedef const Test2::const_it erator Test3;

Test3 foo () { return m_test.begin (); }
void add( const std::string& s, const Test& t )
{
m_test[ s ] = new Test( t );
}

void modify( const std::string& s, const Test& nt )
{
Test* t = const_cast<Test *>( m_test[ s ] );
*t = nt;
}
private:

Test2 m_test;

};

int main ()
{
A a;
A::Test t = {1,2};
a.add( "toto", t );
A::Test3 test = a.foo ();
// Accesses object of private type...
// Didn't want, hoping for compiler error
t.b = 1;
a.modify( "toto", t );
int b = test->second->a;
test->second->a = 1; // modified const...
return 0;
}

Pierre
Aug 14 '06 #2

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

Similar topics

12
1934
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the board. Since this function does not change anything in the class I declared it as const. To count all pieces of a given color the functions iterates through a "map" of CNode-pointers. "CNode" is another class that is irrelevant to the problem.
26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
4
1809
by: NKOBAYE027 | last post by:
Hi Everyone: Short description first MathematicalSet is to be a class template that's supposed to behave as the name suggests. It has functions that define union, contains, is_contained_in, etc... I want the thing to behave like a normal collection as well. That is to comply with the ansi standard for STL collections even though it manages two separate lists and two statics (universe and null). The first collection is an object pool...
7
1842
by: Siemel Naran | last post by:
Hi. I have a function template <class InputIter, class OutputIter> void f(InputIter begin, InputIter end, OutputIter result); With c of type char* and cc of type const char*, the code f(c,c,cc) calls f<char*, const char *>, which is fine. But f(c,c,c) calls a new instantiation f<char*,char*> whereas I'd like it to call f<const char*,char*>.
2
1977
by: John Stiles | last post by:
I have written some pretty simple code which works great in Dev Studio and CodeWarrior, but gcc is giving me link errors. Here is what I've been able to distill it down to: namespace Private { template <class T> struct lengthof; template <class T, size_t N> struct lengthof<T> { static const size_t array_size = N; }; }
5
1905
by: Pedro Sousa | last post by:
Hi, I'm trying to create an template class that represent points in all possible dimensions, what I've made until now is #ifndef _POINT_HPP_ #define _POINT_HPP_ #include <vector>
4
2828
by: Kyku | last post by:
Hello all, I'm in a process of writing a small Linux C++ program for discovering repeaded files over a filesystem. One part of this task is traversing a directory structure. This is done by the following recursive function. Since I'd like it to be general enough to handle function pointers and function objects I've made it a template. template <typename CallBack> void Traverse(const std::string& path, CallBack& callback) throw
0
1940
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from. so here is the error I'm getting: TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
12
6254
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is: gcc version 4.0.1 (Apple Inc. build 5465). thanks for the help. summary of compile error: --------------------------------------- cpp.C:4: error: non-static const member 'const unsigned int A::u',
0
9629
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
9470
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
10298
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...
0
9923
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
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4033
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
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2865
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.