473,508 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

containing one's own type

Related to my last post, but seperate question

Can a class be made into a container of its self?
Could I
Create an attribute class representing a name, value pair
Create an attributeGroup class wrapping a map of attributes
and then add to the attributeGroup class to also wrap a map of
attributeGroups?

That way an attributeGroup could contain n levels of attributes?
Jul 24 '08 #1
14 1269
Christopher wrote:
Related to my last post, but seperate question

Can a class be made into a container of its self?
You're asking, can any box contain several boxes of the same size and
capacity, right? Not in this universe.
Could I
Create an attribute class representing a name, value pair
Create an attributeGroup class wrapping a map of attributes
and then add to the attributeGroup class to also wrap a map of
attributeGroups?

That way an attributeGroup could contain n levels of attributes?
I couldn't get my mind around those English sentences, perhaps you could
put it in C++ (don't worry about compilability of your code for now)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '08 #2
On Jul 24, 1:41 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Christopher wrote:
I couldn't get my mind around those English sentences, perhaps you could
put it in C++ (don't worry about compilability of your code for now)?

V

Conceptual without error handling considered:

// Basic name value pair
class Attribute
{
public:
Attribute();
~Attribute();

const std::string & GetName() const;
void SetName(const std::string & name);

template<typename T>
void SetValue(const T & value)
{
// use boost's lexical cast
}

template<typename T>
void GetValue(const std::string & name, T & value_out)
{
// use boost's lexical cast
}

private:

std::string name;
std::string value;
}

// Group of name value pairs
class AttributeGroup
{
public:
AttributeGroup();
~AttributeGroup();

const std::string & GetName() const;
void SetName(const std::string & name);

void InsertAttribute(const Attribute & attribute);
void RemoveAttribute(const std::string & name);
const Attribute & GetAttribute(const std::string & name) const;

// Questionable Code
void InsertAttributeGroup(const AttributeGroup & group);
void RemoveAttributeGroup(const std::string & name);
const AttributeGroup & GetAttributeGroup(const std::string & name)
const;

private:

std::string name;

typedef std::map<std::string, AttributeAttributeMap;

// Questionable Code
typedef std::map<std::string, AttributeGroupAttributeGroupMap;
}
Jul 24 '08 #3
Victor Bazarov wrote:
Christopher wrote:
>Related to my last post, but seperate question

Can a class be made into a container of its self?

You're asking, can any box contain several boxes of the same size and
capacity, right? Not in this universe.
It depends on your definition of "contain". This is perfectly possible:

class A
{
A* array_of_A[10];

public:
void init()
{
for(int i = 0; i < 10; ++i)
array_of_A[i] = new A;
}
};
Jul 24 '08 #4
Christopher wrote:
On Jul 24, 1:41 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>Christopher wrote:
I couldn't get my mind around those English sentences, perhaps you could
put it in C++ (don't worry about compilability of your code for now)?

V


Conceptual without error handling considered:

// Basic name value pair
class Attribute
{
public:
Attribute();
~Attribute();

const std::string & GetName() const;
void SetName(const std::string & name);

template<typename T>
void SetValue(const T & value)
{
// use boost's lexical cast
}

template<typename T>
void GetValue(const std::string & name, T & value_out)
{
// use boost's lexical cast
}

private:

std::string name;
std::string value;
}

// Group of name value pairs
class AttributeGroup
{
public:
AttributeGroup();
~AttributeGroup();

const std::string & GetName() const;
void SetName(const std::string & name);

void InsertAttribute(const Attribute & attribute);
void RemoveAttribute(const std::string & name);
const Attribute & GetAttribute(const std::string & name) const;

// Questionable Code
void InsertAttributeGroup(const AttributeGroup & group);
void RemoveAttributeGroup(const std::string & name);
const AttributeGroup & GetAttributeGroup(const std::string & name)
const;

private:

std::string name;

typedef std::map<std::string, AttributeAttributeMap;

// Questionable Code
typedef std::map<std::string, AttributeGroupAttributeGroupMap;
}
Make your map not of string-value pairs but of string-pointer pairs and
you can easily contain such maps in the objects.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '08 #5
On Jul 24, 2:56*pm, Christopher <cp...@austin.rr.comwrote:
Related to my last post, but seperate question

Can a class be made into a container of its self?
Of course

#include <vector>
class Test {
int i;
std::vector<TestmyselfVector;
};

int main(int argc, char* argv[])
{
Test t;
}

Jul 24 '08 #6
On Jul 24, 9:45 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
On Jul 24, 2:56 pm, Christopher <cp...@austin.rr.comwrote:
Related to my last post, but seperate question
Can a class be made into a container of its self?
Of course
#include <vector>
class Test {
int i;
std::vector<TestmyselfVector;
};
Which is undefined behavior, and won't compile with g++.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 24 '08 #7
On Jul 24, 5:02*pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 9:45 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
On Jul 24, 2:56 pm, Christopher <cp...@austin.rr.comwrote:
Related to my last post, but seperate question
Can a class be made into a container of its self?
Of course
#include <vector>
class Test {
* * * * int i;
* * * * std::vector<TestmyselfVector;
};

Which is undefined behavior,
I didn't know that.
Where can I find more information? Any paper?
and won't compile with g++.
dario@illusion:~/test$ g++ test.cpp -o showmethemoney
dario@illusion:~/test$ ls -lh
-rwxr-xr-x 1 dario dario 8.6K 2008-07-24 17:12 showmethemoney
-rw-r--r-- 1 dario dario 135 2008-07-24 17:12 test.cpp

dario@illusion:~/test$ g++ --version
g++ (Debian 4.3.1-2) 4.3.1

Jul 24 '08 #8
Juha Nieminen wrote:
Victor Bazarov wrote:
>Christopher wrote:
>>Related to my last post, but seperate question

Can a class be made into a container of its self?
You're asking, can any box contain several boxes of the same size and
capacity, right? Not in this universe.

It depends on your definition of "contain". This is perfectly possible:

class A
{
A* array_of_A[10];

public:
void init()
{
for(int i = 0; i < 10; ++i)
array_of_A[i] = new A;
}
};
That's not containment in the strict sense of the word. That's more
like association+ownership.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '08 #9
Christopher wrote:
Conceptual without error handling considered:

// Basic name value pair
class Attribute
[...]
private:

std::string name;
std::string value;
}
That is a name-value pair...
// Group of name value pairs
class AttributeGroup
{
[...]
private:

std::string name;

typedef std::map<std::string, AttributeAttributeMap;
That would be a named name-value pair. Huh?

So AttributeGroup would be a group of named name-value pairs with a name.
// Questionable Code
typedef std::map<std::string, AttributeGroupAttributeGroupMap;
}
That would be a named (group of ... with a name).

Ok, back to the drafting table.

You need a map which associates names with values. Which would be a
group of name-value pairs, letting you search for a specific name.
The value can be either a string, lexical-casted to whatever type you
want, or a map by itself. Basically this is:

typedef std::map<std::string, either_string_or_namedGroupnamedGroup;

For implementing either_string_or_namedGroup, you could use a base class
with two derived classes, one containing a string and one containing a
namedGroup, and a nice interface to access those.
A recursive boost.variant would do as well and would handle the memory
deallocation issues.

--
Thomas
Jul 24 '08 #10
Darío Griffo wrote:
On Jul 24, 5:02*pm, James Kanze <james.ka...@gmail.comwrote:
>On Jul 24, 9:45 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
On Jul 24, 2:56 pm, Christopher <cp...@austin.rr.comwrote:
Related to my last post, but seperate question
Can a class be made into a container of its self?
Of course
#include <vector>
class Test {
int i;
std::vector<TestmyselfVector;
};

Which is undefined behavior,

I didn't know that.
Where can I find more information? Any paper?
The standard [17.4.3.6/2] last item: you get undefined behavior if you
instantiate vector<with an incomplete type.

>and won't compile with g++.

dario@illusion:~/test$ g++ test.cpp -o showmethemoney
dario@illusion:~/test$ ls -lh
-rwxr-xr-x 1 dario dario 8.6K 2008-07-24 17:12 showmethemoney
-rw-r--r-- 1 dario dario 135 2008-07-24 17:12 test.cpp

dario@illusion:~/test$ g++ --version
g++ (Debian 4.3.1-2) 4.3.1
You probably need to build g++ with --enable-concept-checks or something
like that.

BTW: this shows that library writers have to do some work to make code
violating [17.4.3.6/2] actually fail, and it is not hard to implement
std::vector in such a way that your code will work as expected (in fact, it
is easier to do so than to do otherwise). Nonetheless, the standard is not
likely to change with regard to this item; and I think that
std::shared_ptr<is the only library component for which an exception will
be made.
Best

Kai-Uwe Bux
Jul 24 '08 #11
Christopher wrote:
Related to my last post, but seperate question

Can a class be made into a container of its self?
Yes. I have a pure_finite_set class in my library whose values are the
finite sets from set theory without atoms. So, something like

{ {}, { {}, {{}} } }

is a valid value. A pure_finite_set _is_ nothing but a finite set of
pure_finite_set values.

Could I
Create an attribute class representing a name, value pair
Create an attributeGroup class wrapping a map of attributes
and then add to the attributeGroup class to also wrap a map of
attributeGroups?

That way an attributeGroup could contain n levels of attributes?
Huh? What is the problem you are trying to solve? It appears that you are
going down a path of increasing complexity.
Best

Kai-Uwe Bux
Jul 24 '08 #12
On Jul 24, 10:16 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
On Jul 24, 5:02 pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 9:45 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
On Jul 24, 2:56 pm, Christopher <cp...@austin.rr.comwrote:
Related to my last post, but seperate question
Can a class be made into a container of its self?
Of course
#include <vector>
class Test {
int i;
std::vector<TestmyselfVector;
};
Which is undefined behavior,
I didn't know that.
Where can I find more information? Any paper?
The standard. More generally, however, any textbook concerning
the STL should point out that you're only allowed to instantiate
it for complete types (which is, in fact, true of every template
currently in the standard).
and won't compile with g++.

dario@illusion:~/test$ g++ test.cpp -o showmethemoney
dario@illusion:~/test$ ls -lh
-rwxr-xr-x 1 dario dario 8.6K 2008-07-24 17:12 showmethemoney
-rw-r--r-- 1 dario dario 135 2008-07-24 17:12 test.cpp
dario@illusion:~/test$ g++ --version
g++ (Debian 4.3.1-2) 4.3.1
By default, g++ (like all other compilers I know) doesn't
implement C++, but rather a somewhat similar, but not identical,
language. For everyday use, I use "g++ -std=c++98 -pedantic
-ffor-scope -fno-gnu-keywords -foperator-names -pipe -Wall -W
-Woverloaded-virtual -Wno-sign-compare -Wno-deprecated
-Wno-non-virtual-dtor -Wpointer-arith -Wno-unused -Wno-switch
-Wno-missing-braces -Wno-long-long -static-libgcc -ggdb3
-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC". (Some of the options represent
personal preferences, of course, and some correspond to
constraints due to the code I work with -- -Wno-long-long, for
example. But as a very minimum, if you're trying to work in
standard C++, you need -std=c++98 -pedantic
-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC, and perhaps -ffor-scope
-fno-gnu-keywords.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #13
On Jul 25, 1:11 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Darío Griffo wrote:
[...]
You probably need to build g++ with --enable-concept-checks or
something like that.
Perhaps -D_GLIBCXX_CONCEPT_CHECKS? (I think that
--enable-concept-checks is only available in some special
builds.)
BTW: this shows that library writers have to do some work to
make code violating [17.4.3.6/2] actually fail, and it is not
hard to implement std::vector in such a way that your code
will work as expected (in fact, it is easier to do so than to
do otherwise). Nonetheless, the standard is not likely to
change with regard to this item;
Actually, I think it will change: since concepts are being added
(I hope), instead of undefined behavior, the error will require
a diagnostic.
and I think that std::shared_ptr<is the only library
component for which an exception will be made.
For the moment, that's what I understand as well. (Although
some other constraints are also being loosened: std::list won't
require Assignable, if I'm not mistaken.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #14
On Jul 25, 9:06*am, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 10:16 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
On Jul 24, 5:02 pm, James Kanze <james.ka...@gmail.comwrote:
On Jul 24, 9:45 pm, Darío Griffo <dario.griffo.lis...@gmail.com>
wrote:
On Jul 24, 2:56 pm, Christopher <cp...@austin.rr.comwrote:
Related to my last post, but seperate question
Can a class be made into a container of its self?
Of course
#include <vector>
class Test {
* * * * int i;
* * * * std::vector<TestmyselfVector;
};
Which is undefined behavior,
I didn't know that.
Where can I find more information? Any paper?

The standard. *More generally, however, any textbook concerning
the STL should point out that you're only allowed to instantiate
it for complete types (which is, in fact, true of every template
currently in the standard).
and won't compile with g++.
dario@illusion:~/test$ g++ test.cpp -o showmethemoney
dario@illusion:~/test$ ls -lh
-rwxr-xr-x 1 dario dario 8.6K 2008-07-24 17:12 showmethemoney
-rw-r--r-- 1 dario dario *135 2008-07-24 17:12 test.cpp
dario@illusion:~/test$ g++ --version
g++ (Debian 4.3.1-2) 4.3.1

By default, g++ (like all other compilers I know) doesn't
implement C++, but rather a somewhat similar, but not identical,
language. *For everyday use, I use "g++ -std=c++98 -pedantic
-ffor-scope -fno-gnu-keywords -foperator-names -pipe -Wall -W
-Woverloaded-virtual -Wno-sign-compare -Wno-deprecated
-Wno-non-virtual-dtor -Wpointer-arith -Wno-unused -Wno-switch
-Wno-missing-braces -Wno-long-long -static-libgcc -ggdb3
-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC". *(Some of the options represent
personal preferences, of course, and some correspond to
constraints due to the code I work with -- -Wno-long-long, for
example. *But as a very minimum, if you're trying to work in
standard C++, you need -std=c++98 -pedantic
-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC, and perhaps -ffor-scope
-fno-gnu-keywords.)

Thanks a lot James, and also Kai-Uwe Bux for the complete explanation.
It's that every day we learn new things.
Darío
Jul 25 '08 #15

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

Similar topics

2
1564
by: Good Enchiladas | last post by:
While building on a class library for an object model, I get the above error message. The steps to recreate the problem are as follows: 1. Build a RootLevel.dll containing only this code: ...
6
4174
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
3
2821
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
5
2934
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate...
10
4968
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
4
2565
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
15
3503
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
11
1342
by: John A Grandy | last post by:
I'm in a vigorous debate at my work regarding objects assuming knowledge of the type their containing object. This debate pertains specifically to ASP.NET, but I have decided to post in the C#...
3
1906
by: Warren DeLano | last post by:
I would like to parse arbitrary insecure text string containing nested Python data structures in eval-compatible form: # For example, given a "config.txt" such as: { 'my_atom' : 1.20,...
0
7398
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...
1
7061
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...
0
7502
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...
0
5637
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,...
0
4716
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...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1566
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 ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
428
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...

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.