Is there a cleaner way to do this?

P

Peter Olcott

//
// I would like to some how avoid using the global ONE
// without incurring any performance penalty in speed or space
//

#include <vector>
#include <algorithm>

struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
} ONE;

struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
};

bool TwoType::eek:perator()(const int& N, const int& M) {
return ONE[N] < ONE[M];
}
 
G

Gernot Frisch

Peter Olcott said:
//
// I would like to some how avoid using the global ONE
// without incurring any performance penalty in speed or space
//

#include <vector>
#include <algorithm>


class OneTwo
{
public:

struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
}

m_ONE;


struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
};

bool TwoType::eek:perator()(const int& N, const int& M) {
return m_ONE[N] < m_ONE[M];
}
};
 
P

Peter Olcott

That looks like just the sort of solution that I was looking for.
Thanks again.

Gernot Frisch said:
Peter Olcott said:
//
// I would like to some how avoid using the global ONE
// without incurring any performance penalty in speed or
space
//

#include <vector>
#include <algorithm>


class OneTwo
{
public:

struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB <
Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
}

m_ONE;


struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
};

bool TwoType::eek:perator()(const int& N, const int& M) {
return m_ONE[N] < m_ONE[M];
}
};
 
P

Peter Olcott

The code will not compile.

Gernot Frisch said:
Peter Olcott said:
//
// I would like to some how avoid using the global ONE
// without incurring any performance penalty in speed or
space
//

#include <vector>
#include <algorithm>


class OneTwo
{
public:

struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB <
Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
}

m_ONE;


struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
};

bool TwoType::eek:perator()(const int& N, const int& M) {
return m_ONE[N] < m_ONE[M];
}
};

#include <stdio.h>
#include <vector>
#include <algorithm>


class OneTwo {
public:

struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB <
Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
} ONE;

struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M)
{ return ONE[N] < ONE[M]; };
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); };
};

//std::vector<TwoType> StackTwoType;
};



void main() {
}


t.cpp
t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
class is not a type name, static, or enumerator
t.cpp(24) : error C2065: 'ONE' : undeclared identifier
t.cpp(24) : error C2109: subscript requires array or pointer type
t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
class is not a type name, static, or enumerator
t.cpp(24) : error C2109: subscript requires array or pointer type
t.cpp(35) : error C2143: syntax error : missing ';' before '.'
t.cpp(35) : error C2501: 't' : missing storage-class or type
specifiers
t.cpp(35) : error C2143: syntax error : missing ';' before '.'
 
V

Victor Bazarov

Peter said:
The code will not compile.

//
// I would like to some how avoid using the global ONE
// without incurring any performance penalty in speed or
space
//

#include <vector>
#include <algorithm>


class OneTwo
{
public:


struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB <
Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
}

m_ONE;


struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
};

bool TwoType::eek:perator()(const int& N, const int& M) {
return m_ONE[N] < m_ONE[M];
}

};


#include <stdio.h>
#include <vector>
#include <algorithm>


class OneTwo {
public:

struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB <
Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
} ONE;

Here 'ONE' is declared a member of 'OneTwo'.
struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M)
{ return ONE[N] < ONE[M]; };

Here you try to use 'ONE' inside of 'TwoType' (which is unrelated to
'OneTwo', only defined inside it). Of course 'ONE' is undeclared here.

Are you coming from Javaland, where members of enclosing class are also
members of the inner classes and instances of inner classes are also
data members of the enclosing classes? It's not the C++ way. You need
an instance of 'OneTwo' to access its members.
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); };
};

//std::vector<TwoType> StackTwoType;
};



void main() {
}


t.cpp
t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
class is not a type name, static, or enumerator
t.cpp(24) : error C2065: 'ONE' : undeclared identifier
t.cpp(24) : error C2109: subscript requires array or pointer type
t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
class is not a type name, static, or enumerator
t.cpp(24) : error C2109: subscript requires array or pointer type
t.cpp(35) : error C2143: syntax error : missing ';' before '.'
t.cpp(35) : error C2501: 't' : missing storage-class or type
specifiers
t.cpp(35) : error C2143: syntax error : missing ';' before '.'

V
 
P

Peter Olcott

I am still working on the same problem that I talked to you about
last night. This code was the suggestion of Gernot Frisch.

Victor Bazarov said:
Peter said:
The code will not compile.

//
// I would like to some how avoid using the global ONE
// without incurring any performance penalty in speed or
space
//

#include <vector>
#include <algorithm>


class OneTwo
{
public:



struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB <
Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
}


m_ONE;


struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
};

bool TwoType::eek:perator()(const int& N, const int& M) {
return m_ONE[N] < m_ONE[M];
}

};


#include <stdio.h>
#include <vector>
#include <algorithm>


class OneTwo {
public:

struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB <
Text.BBB; };
};

struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
} ONE;

Here 'ONE' is declared a member of 'OneTwo'.
struct TwoType {
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M)
{ return ONE[N] < ONE[M]; };

Here you try to use 'ONE' inside of 'TwoType' (which is
unrelated to
'OneTwo', only defined inside it). Of course 'ONE' is
undeclared here.

Are you coming from Javaland, where members of enclosing class
are also
members of the inner classes and instances of inner classes are
also
data members of the enclosing classes? It's not the C++ way.
You need
an instance of 'OneTwo' to access its members.
void sort(){ std::sort(Sub.begin(), Sub.end(),
(*this)); };
};

//std::vector<TwoType> StackTwoType;
};



void main() {
}


t.cpp
t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
class is not a type name, static, or enumerator
t.cpp(24) : error C2065: 'ONE' : undeclared identifier
t.cpp(24) : error C2109: subscript requires array or pointer
type
t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
class is not a type name, static, or enumerator
t.cpp(24) : error C2109: subscript requires array or pointer
type
t.cpp(35) : error C2143: syntax error : missing ';' before '.'
t.cpp(35) : error C2501: 't' : missing storage-class or type
specifiers
t.cpp(35) : error C2143: syntax error : missing ';' before '.'

V
 
G

Gernot Frisch

class OneTwo
{
struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
};


struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
}m_ONE;

struct TwoType
{
TwoType(OneType& ot): m_ot(ot) {}
OneType& m_ot;
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort() { std::sort(Sub.begin(), Sub.end(), (*this)); }
};
};

bool OneTwo::TwoType::eek:perator()(const int& N, const int& M)
{
return m_ot[N] < m_ot[M];
}


Now you have to provide a OneType when creating a TwoType. Can you
live with that?
 
P

Peter Olcott

Gernot Frisch said:
class OneTwo
{
struct RecType {
int AAA;
int BBB;
int CCC;
bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
};


struct OneType {
std::vector<RecType> Record;
RecType& operator[](const int N){ return Record[N]; };
}m_ONE;

struct TwoType
{
TwoType(OneType& ot): m_ot(ot) {}
OneType& m_ot;
std::vector<int> Sub; // Subscripts of ONE
bool operator()(const int& N, const int& M);
void sort() { std::sort(Sub.begin(), Sub.end(), (*this)); }
};
};

bool OneTwo::TwoType::eek:perator()(const int& N, const int& M)
{
return m_ot[N] < m_ot[M];
}


Now you have to provide a OneType when creating a TwoType. Can
you live with that?

I am not sure. If it would actually construct a copy of my
OneType it won't be feasible. OneType is essentially a singleton.
It can have hundreds of megabytes of data. If what you are
proposing is the same thing as copying a pointer, then it could
work, yet might not be worth the extra overhead. From what I
understand references can work like pointers yet are processed at
compile time, rather than run-time. What I have done in the mean
time is to rename OneType to GlobalOneType, and then passed this
as a parameter. In other words I have minimized the global access
to GlobalOneType, and made the remaining access explicitly clear
that it is global.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top