473,394 Members | 1,722 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,394 software developers and data experts.

classes in a class

I have a class that performs transformations on data streams. As written,
the class constructor takes several parameters and a data-source to produce
it's stream.

I have the need to combine the output of two instances of this class to
generate a third data stream. (think eg of applying a filters to audio)

When I do this, the parameters that I supply the second class are dependent
upon the state of the system after the first transform has occured.

I have been accomplishing this by using a function outside the class that
instantiates the two classes as the information needed to instantiate them
becomes available. Once both objects are built, their output is combined
and the function exits, the objects are destroyed, and the caller gets his
transformed data.

I don't like this solution because my objects cease to exist once I return
the data...but I want to keem them around for a while longer.

So...as a solution, I decided to create a new class that would hold the two
instances of my transforming class objects and return the combined data. My
problem is...how can I construct my two member-data objects when the
construction parameters of the second object depend upon having made some
calculations on the first, successfully constructed object?

I can't supply the constructor parameters for the second object in the
initialization list because those parameters require some processing *after*
the first object is constructed.

Thanks for your thoughts

Joe

Jul 22 '05 #1
6 1025
Joe C wrote:
I have a class that performs transformations on data streams. As written,
the class constructor takes several parameters and a data-source to produce
it's stream.

I have the need to combine the output of two instances of this class to
generate a third data stream. (think eg of applying a filters to audio)

When I do this, the parameters that I supply the second class are dependent
upon the state of the system after the first transform has occured.

I have been accomplishing this by using a function outside the class that
instantiates the two classes as the information needed to instantiate them
becomes available. Once both objects are built, their output is combined
and the function exits, the objects are destroyed, and the caller gets his
transformed data.

I don't like this solution because my objects cease to exist once I return
the data...but I want to keem them around for a while longer.

So...as a solution, I decided to create a new class that would hold the two
instances of my transforming class objects and return the combined data. My
problem is...how can I construct my two member-data objects when the
construction parameters of the second object depend upon having made some
calculations on the first, successfully constructed object?

I can't supply the constructor parameters for the second object in the
initialization list because those parameters require some processing *after*
the first object is constructed.


Make the second subobject a dynamic one and construct it using 'new':

class myclass {
myfirstauxiliaryclass aux1;
mysecondauxiliaryclass *paux2;
public:
myclass() : aux1(parameters) {
somecalculations(aux1);
paux2 = new mysecondauxiliaryclass(otherparameters);
}
};

Victor
Jul 22 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message > Make the
second subobject a dynamic one and construct it using 'new':

class myclass {
myfirstauxiliaryclass aux1;
mysecondauxiliaryclass *paux2;
public:
myclass() : aux1(parameters) {
somecalculations(aux1);
paux2 = new mysecondauxiliaryclass(otherparameters);
}
};

Victor


Thanks Victor. This is exactly what I need. I guess it should have been
obvious to me but...well...it wasn't 8-\ Thanks again for 'getting' my
question, and giving me a straight forward solution.
Jul 22 '05 #3
"Joe C" <jk*****@bellsouth.net> wrote in message
news:w0*********************@bignews1.bellsouth.ne t...

"Victor Bazarov" <v.********@comAcast.net> wrote in message > Make the
second subobject a dynamic one and construct it using 'new':

class myclass {
myfirstauxiliaryclass aux1;
mysecondauxiliaryclass *paux2;
public:
myclass() : aux1(parameters) {
somecalculations(aux1);
paux2 = new mysecondauxiliaryclass(otherparameters);
}
};

Victor


Thanks Victor. This is exactly what I need. I guess it should have been
obvious to me but...well...it wasn't 8-\ Thanks again for 'getting' my
question, and giving me a straight forward solution.


It isn't quite as straight forward as you might think. The code as written
has a memory leak -- operator delete is never called. Of course you can add
a destructor with a delete statement, but then you really should add a copy
constructor and an assignment operator, remembering of course to avoid the
x=x bug in an exception-safe matter. No big deal, but still...

You could use a smart pointer, but it can also be done without any pointers
at all:

class two_things
{
private:
thing a;
thing b;
public:
two_things(arg_type arg) : a(arg), b(a) {}
// other stuff
};

This may look dangerous, but the standard guarantees that member variables
are intialized in the order they are declared. Thus a is constructed before
b and is safe to use as an argument for b's constructor.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote...
"Joe C" <jk*****@bellsouth.net> wrote in message
news:w0*********************@bignews1.bellsouth.ne t...

"Victor Bazarov" <v.********@comAcast.net> wrote in message > Make the
second subobject a dynamic one and construct it using 'new':
>
> class myclass {
> myfirstauxiliaryclass aux1;
> mysecondauxiliaryclass *paux2;
> public:
> myclass() : aux1(parameters) {
> somecalculations(aux1);
> paux2 = new mysecondauxiliaryclass(otherparameters);
> }
> };
>
> Victor


Thanks Victor. This is exactly what I need. I guess it should have been
obvious to me but...well...it wasn't 8-\ Thanks again for 'getting' my
question, and giving me a straight forward solution.


It isn't quite as straight forward as you might think. The code as written
has a memory leak -- operator delete is never called. Of course you can
add
a destructor with a delete statement, but then you really should add a
copy
constructor and an assignment operator, remembering of course to avoid the
x=x bug in an exception-safe matter. No big deal, but still...

You could use a smart pointer, but it can also be done without any
pointers
at all:

class two_things
{
private:
thing a;
thing b;
public:
two_things(arg_type arg) : a(arg), b(a) {}
// other stuff
};

This may look dangerous, but the standard guarantees that member variables
are intialized in the order they are declared. Thus a is constructed
before
b and is safe to use as an argument for b's constructor.


Yes, and to perform that extra calculation you could declare a dummy member
and initialise it with the value returned by that calculation function:

class two_things
{
thing_one a;
int dummy;
thing_two b;

int some_extra_calculations(thing_one&);

public:
two_things(some_arguments)
: a(whatever)
, dummy(some_extra_calculations(a)
, b(something_else)
{
}
};

Given certain assumptions, everything is possible.

V
Jul 22 '05 #5

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:n3*********************@twister.nyroc.rr.com. ..
"Joe C" <jk*****@bellsouth.net> wrote in message
news:w0*********************@bignews1.bellsouth.ne t...

"Victor Bazarov" <v.********@comAcast.net> wrote in message > Make the
second subobject a dynamic one and construct it using 'new':
>
> class myclass {
> myfirstauxiliaryclass aux1;
> mysecondauxiliaryclass *paux2;
> public:
> myclass() : aux1(parameters) {
> somecalculations(aux1);
> paux2 = new mysecondauxiliaryclass(otherparameters);
> }
> };
>
> Victor
Thanks Victor. This is exactly what I need. I guess it should have been
obvious to me but...well...it wasn't 8-\ Thanks again for 'getting' my
question, and giving me a straight forward solution.


It isn't quite as straight forward as you might think. The code as written
has a memory leak -- operator delete is never called. Of course you can
add
a destructor with a delete statement, but then you really should add a
copy
constructor and an assignment operator, remembering of course to avoid the
x=x bug in an exception-safe matter. No big deal, but still...


Thanks for the comments. I used Vics method, and...as I guess Victor
guessed, we were on the same page regarding cleaning up memory.

You could use a smart pointer, but it can also be done without any
pointers
at all:

class two_things
{
private:
thing a;
thing b;
public:
two_things(arg_type arg) : a(arg), b(a) {}
// other stuff
};

This may look dangerous, but the standard guarantees that member variables
are intialized in the order they are declared. Thus a is constructed
before
b and is safe to use as an argument for b's constructor.

I don't think this technique will work, unless I modify the 'thing' class to
do the calculations that b needed...which doesn't make logical sense for
this class. I can't use your method because the method of generating the
parameters needed to instantiate b from a requires about 10 lines of code.
Anyway, thanks for offering another approach.
Jul 22 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:t8t2d.103477$3l3.48115@attbi_s03...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote...
"Joe C" <jk*****@bellsouth.net> wrote in message
news:w0*********************@bignews1.bellsouth.ne t...

"Victor Bazarov" <v.********@comAcast.net> wrote in message > Make the
second subobject a dynamic one and construct it using 'new':
>
> class myclass {
> myfirstauxiliaryclass aux1;
> mysecondauxiliaryclass *paux2;
> public:
> myclass() : aux1(parameters) {
> somecalculations(aux1);
> paux2 = new mysecondauxiliaryclass(otherparameters);
> }
> };
>
> Victor

Thanks Victor. This is exactly what I need. I guess it should have
been
obvious to me but...well...it wasn't 8-\ Thanks again for 'getting' my
question, and giving me a straight forward solution.


It isn't quite as straight forward as you might think. The code as
written
has a memory leak -- operator delete is never called. Of course you can
add
a destructor with a delete statement, but then you really should add a
copy
constructor and an assignment operator, remembering of course to avoid
the
x=x bug in an exception-safe matter. No big deal, but still...

You could use a smart pointer, but it can also be done without any
pointers
at all:

class two_things
{
private:
thing a;
thing b;
public:
two_things(arg_type arg) : a(arg), b(a) {}
// other stuff
};

This may look dangerous, but the standard guarantees that member
variables
are intialized in the order they are declared. Thus a is constructed
before
b and is safe to use as an argument for b's constructor.


Yes, and to perform that extra calculation you could declare a dummy
member
and initialise it with the value returned by that calculation function:

class two_things
{
thing_one a;
int dummy;
thing_two b;

int some_extra_calculations(thing_one&);

public:
two_things(some_arguments)
: a(whatever)
, dummy(some_extra_calculations(a)
, b(something_else)
{
}
};

Given certain assumptions, everything is possible.

V


The class actually instantiates with a struct and a container ...the dummy
method would have gotten a little ugly. For this particular problem, I
think the pointer method you first offered was the more natural approach
compared to forcing it to happen in the initialization list. The only
downside is that one "thing" gets.treatment() while the other "thing"
gets->treatment() which is slightly awkward to have two very similar things
requiring different syntax. However, as this is hidden in the two_things
class, it doesn't really matter much.

Thanks guys.
Jul 22 '05 #7

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

Similar topics

12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
6
by: mailforpr | last post by:
Suppose you have a couple of helper classes that are used by 2 client classes only. How can I hide these helper classes from other programmers? Do you think this solution is a good idea?: class...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...

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.