473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

is this possible?

Kev
Hi, lets see if I can explain this right. :o)

Lets say I wish to change several variables nested multiple classes deep.
The usual way I figure is to just say "class#.struct.a (b,c...) =
somenumbers;". Is there a way to preset "class#' at the beginning of the
function its passed to so that I can just say 'struct.a (b,c) = numbers'
and "class#' is assumed?

cheers
Jul 23 '05 #1
8 1292
"Kev" <di**************@pht.zzz> wrote in message
news:Xn**********************@216.168.3.44...
Hi, lets see if I can explain this right. :o)

Lets say I wish to change several variables nested multiple classes deep.
The usual way I figure is to just say "class#.struct.a (b,c...) =
somenumbers;".
I'm assuming you really mean multiple _objects_ deep.
Is there a way to preset "class#' at the beginning of the
function its passed to so that I can just say 'struct.a (b,c) = numbers'
and "class#' is assumed?


Only if the function is a non-static member function of the class of which
the object class# is an instance.

DW
Jul 23 '05 #2
"Kev" <di**************@pht.zzz> wrote in message
news:Xn**********************@216.168.3.44
Hi, lets see if I can explain this right. :o)

Lets say I wish to change several variables nested multiple classes
deep. The usual way I figure is to just say "class#.struct.a (b,c...)
= somenumbers;". Is there a way to preset "class#' at the beginning
of the function its passed to so that I can just say 'struct.a (b,c)
= numbers' and "class#' is assumed?

cheers


Given an outer object (or a reference or pointer to it), you can create a
reference (or pointer) to an inner object, e.g.,
struct Outer
{
struct Inner
{
struct DoubleInner
{
int a, b, c;
};
DoubleInner doubleinner;
};
Inner inner;
};

void Setabc(Outer &o)
{
Outer::Inner::DoubleInner & shortcut = o.inner.doubleinner;
shortcut.a = 1;
shortcut.b = 2;
shortcut.c = 3;
}

The other way to do it is to make Setabc take a reference to DoubleInner as
its argument:

void Setabc(Outer::Inner::DoubleInner &di)
{
di.a = 1;
di.b = 2;
di.c = 3;
}

You would then have to supply, say, o.inner.doubleinner as an argument to
the function.

--
John Carson

Jul 23 '05 #3

"Kev" <di**************@pht.zzz> schrieb im Newsbeitrag
news:Xn**********************@216.168.3.44...
Hi, lets see if I can explain this right. :o)

Lets say I wish to change several variables nested multiple classes
deep.
The usual way I figure is to just say "class#.struct.a (b,c...) =
somenumbers;". Is there a way to preset "class#' at the beginning of
the
function its passed to so that I can just say 'struct.a (b,c) =
numbers'
and "class#' is assumed?


class A
{
public:
int m;
};
class B
{
public:
double m;
}
template <class C> changeM(C* pC)
{
pC->m = 0;
}

int main()
{
A a; B b;
changeM(&a);
changeM(&b);
}

Is this what you want??
Jul 23 '05 #4
Kev
"John Carson" <jc****************@netspace.net.au> wrote in news:daii34
$1*****@otis.netspace.net.au:
void Setabc(Outer &o)
{
Outer::Inner::DoubleInner & shortcut = o.inner.doubleinner;
shortcut.a = 1;
shortcut.b = 2;
shortcut.c = 3;
}


Would this work if Outer could represent more than one classtype, but
sharing the same base? Perhaps using the base classname instead? Similar to
using a base pointer for a derived class? In this case DoubleInner would be
in the base, common to all.

Base::Inner::DoubleInner &shortcut ?
Jul 23 '05 #5
"Kev" <di**************@pht.zzz> wrote in message
news:Xn**********************@216.168.3.44
"John Carson" <jc****************@netspace.net.au> wrote in
news:daii34 $1*****@otis.netspace.net.au:
void Setabc(Outer &o)
{
Outer::Inner::DoubleInner & shortcut = o.inner.doubleinner;
shortcut.a = 1;
shortcut.b = 2;
shortcut.c = 3;
}


Would this work if Outer could represent more than one classtype, but
sharing the same base? Perhaps using the base classname instead?
Similar to using a base pointer for a derived class? In this case
DoubleInner would be in the base, common to all.


Do you mean the class DoubleInner or the variable doubleinner or both? If
Outer is a base class, say:

struct Derived1 : public Outer
{
// stuff
};

struct Derived2 : public Outer
{
// stuff
}

and we have an instance of a derived class:

Derived1 d1;

then we can call the original function with no change:

Setabc(d1);

All the action is in the base class and the fact that it is derived from is
irrelevant.

The more general point is this: if you can get access to member variables
within the function via a series of objects/pointers:

a.b->c.d.e->f.g.member1
a.b->c.d.e->f.g.member2
a.b->c.d.e->f.g.member3

then you can produce a shortcut with which to access members 1-3 as follows:

A::B::C::D::E::F::G & shortcut = a.b->c.d.e->f.g;

where A is the type of a, B* is the type of b, C is the type of c and so on.

You then call it with

shortcut.member1
shortcut.member2
shortcut.member3

If the last thing in the original chain before the members is a pointer
rather than an object, i.e.,

a.b->c.d.e->f.g->member1
a.b->c.d.e->f.g->member2
a.b->c.d.e->f.g->member3

then the definition of shortcut changes via the addition of an asterisk so
that it becomes a reference to a pointer:

A::B::C::D::E::F::G *& shortcut = a.b->c.d.e->f.g;

(note that pointers earlier in the chain affect the right-hand side of the
assignment, but not the left-hand side; by contrast, if the final thing in
the chain is a pointer, it affects the left-hand side but not the right-hand
side). You use shortcut as follows:

shortcut->member1
shortcut->member2
shortcut->member3

You can also transform a pointer into a reference or a reference into a
pointer. This is left as an exercise for the reader.

--
John Carson

Jul 23 '05 #6
Kev
"John Carson" <jc****************@netspace.net.au> wrote in news:dal1e5
$2*****@otis.netspace.net.au:
A::B::C::D::E::F::G & shortcut = a.b->c.d.e->f.g;


I get compiler errors with A::B saying that B is not a member of A. So...
Im sure Im making some 'bite me in the ass' mistake someplace that I will
find ;o)

I appreciate everyones efforts and sharing different ways to figure this
out. After reading further using 'namespace' in some more elaborate ways
might offer something. But I might have misunderstood and need to read
further.

cheers all!!!
Jul 23 '05 #7
"Kev" <di**************@pht.zzz> wrote in message
news:Xn*********************@216.168.3.44
"John Carson" <jc****************@netspace.net.au> wrote in
news:dal1e5 $2*****@otis.netspace.net.au:
A::B::C::D::E::F::G & shortcut = a.b->c.d.e->f.g;


I get compiler errors with A::B saying that B is not a member of A.
So... Im sure Im making some 'bite me in the ass' mistake someplace
that I will find ;o)


My description assumes that B is a class/struct nested inside A, that C is a
class/struct nested inside B and so on. Taking a two struct case for
simpliciy, if, say, you had this scenario:

struct B
{
int member1, member2;
};

struct A
{
B b;
};

Then clearly given

A a;

a.b gives you an object of B, not an object of A::B. Thus you would use:

B & shortcut = a.b;

shortcut.member1
shortcut.member2

Basically, given

a.b->c.d.e->f.g.member1

you have to figure out the type of g. You then declare:

Type_of_g & shortcut = a.b->c.d.e->f.g;
--
John Carson

Jul 23 '05 #8
Kev
"John Carson" <jc****************@netspace.net.au> wrote in news:dalc5p
$2*****@otis.netspace.net.au:
you have to figure out the type of g. You then declare:

Type_of_g & shortcut = a.b->c.d.e->f.g;


Apologies, was away for awhile. After thinking about the problem I really
was making it more difficult than I needed to. Many things just werent in
the right place to begin with. The shortcut ideas are less complicated,
done a little differently, and work nicely.

Thank you :o)

Theres another puzzler about stl::list, and also something Im going about
the wrong way Im sure. But thats another post :o)
Jul 23 '05 #9

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

Similar topics

4
by: Julia Briggs | last post by:
I am struggling to create a PHP function that would take a specified image (JPG, GIF or PNG) from a link, and resize it down to a thumbnail so it will always fit in a 200x250 space. I am hoping...
36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
20
by: CHIN | last post by:
Hi all.. here s my problem ( maybe some of you saw me on other groups, but i cant find the solution !! ) I have to upload a file to an external site, so, i made a .vbs file , that logins to...
7
by: Andrzej | last post by:
Is it possible to call a function which name is given by a string? Let assume that I created a program which call some functions for example void f1(void), void f2(void), void f3(void). ...
2
by: Bhupesh Naik | last post by:
This is a query regarding my problem to make a spell and grammar check possible in text area of a web page. We have aspx pages which are used to construct letters. The browser based screens...
1
by: AAA | last post by:
hi, I'll explain fastly the program that i'm doing.. the computer asks me to enter the cardinal of a set X ( called "dimX" type integer)where X is a table of one dimension and then to fill it...
25
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
4
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate...
7
by: Robert S. | last post by:
Searching some time now for documents on this but still did not find anything about it: Is it possible to replace the entry screen of MS Office Access 2007 - that one presenting that default...
14
by: bjorklund.emil | last post by:
Hello pythonistas. I'm a newbie to pretty much both programming and Python. I have a task that involves writing a test script for every possible combination of preference settings for a software...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.