473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Interdependent classes

I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:

layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers[i] = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers[i] = &Layer(layers[i - 1], layers[i + 1], numNodes[i]);
layers[i] = &Layer(layers[i - 1], NULL, numNodes[i]);

Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?

May 5 '07 #1
5 1741
"MathStuf" <Ma******@gmail.comha scritto nel messaggio
news:11**********************@e65g2000hsc.googlegr oups.com...
>I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:

layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers[i] = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers[i] = &Layer(layers[i - 1], layers[i + 1], numNodes[i]);
layers[i] = &Layer(layers[i - 1], NULL, numNodes[i]);

Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?
You can't execute the constructor when you want to... it has to be executed
when the class is instantiated; you can allocate the space for the class
without running it (what you're doing with malloc()), but then you have no
way to manually run the constructor. By the way, creating such unconstructed
classes is something you really should avoid.

If you need to initialize a class after its instantiation, add to it an
Init() method that you can call whenever you need it.

Anyway, why do you need to allocate space and run constructors at different
times?

Your code should be

layers = new Layer*[numLayers];
layers[0] = new Layer(NULL,layers[1],numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers[i] = new Layer(layers[i - 1],layers[i + 1],numNodes[i]);
layers[i] = new Layer(layers[i - 1],NULL,numNodes[i]);

Massimo

May 5 '07 #2
MathStuf wrote:
I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:

layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers[i] = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers[i] = &Layer(layers[i - 1], layers[i + 1], numNodes[i]);
layers[i] = &Layer(layers[i - 1], NULL, numNodes[i]);

Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?

I can't tell what you're trying to do.

Maybe this:

Layer * layers = (Layer *) malloc(numLayers * sizeof(Layer));

new (layers +0) Layer(NULL, layers+0, numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i) {
new (layers + i) Layer(layers+i-1, layers+i+1, numNodes[i]);
}
new (layers + i) Layer(layers[i - 1], NULL, numNodes[i]);

.... remember to destruct everything - in reverse.

layers[numLayers - 1].~Layer();
for (int i = numLayers - 1; i >= 0; --i) {
layers[i].~Layer();
}

free(layers);
May 5 '07 #3
On May 4, 9:25 pm, "Massimo" <bar...@mclink.itwrote:
"MathStuf" <MathS...@gmail.comha scritto nel messaggionews:11**********************@e65g2000hsc .googlegroups.com...
I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:
layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers[i] = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers[i] = &Layer(layers[i - 1], layers[i + 1], numNodes[i]);
layers[i] = &Layer(layers[i - 1], NULL, numNodes[i]);
Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?

You can't execute the constructor when you want to... it has to be executed
when the class is instantiated; you can allocate the space for the class
without running it (what you're doing with malloc()), but then you have no
way to manually run the constructor. By the way, creating such unconstructed
classes is something you really should avoid.

If you need to initialize a class after its instantiation, add to it an
Init() method that you can call whenever you need it.

Anyway, why do you need to allocate space and run constructors at different
times?

Your code should be

layers = new Layer*[numLayers];
layers[0] = new Layer(NULL,layers[1],numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers[i] = new Layer(layers[i - 1],layers[i + 1],numNodes[i]);
layers[i] = new Layer(layers[i - 1],NULL,numNodes[i]);

Massimo
Ah...forgot about 'new' (I'm not new (no pun intended) to C++, but I
haven't needed it much so far). Thanks.

--MathStuf

May 5 '07 #4
MathStuf wrote:
On May 4, 9:25 pm, "Massimo" <bar...@mclink.itwrote:
....
Ah...forgot about 'new' (I'm not new (no pun intended) to C++, but I
haven't needed it much so far). Thanks.
To say that you must be missing somthing. The new operator is an
important part of the language.

May 5 '07 #5
Gianni Mariani wrote:
....
>

I can't tell what you're trying to do.

Maybe this:

Layer * layers = (Layer *) malloc(numLayers * sizeof(Layer));

new (layers +0) Layer(NULL, layers+0, numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i) {
new (layers + i) Layer(layers+i-1, layers+i+1, numNodes[i]);
}
new (layers + i) Layer(layers[i - 1], NULL, numNodes[i]);
new (layers + i) Layer(layers+i-1, NULL, numNodes[i]);
fixed - should be pointer arithmetic not array reference
>
... remember to destruct everything - in reverse.

layers[numLayers - 1].~Layer();
^^^^^^^^^^^^^^^^^^ forgot to remove this line ........

Save someone else from picking this errors up ....
for (int i = numLayers - 1; i >= 0; --i) {
layers[i].~Layer();
}

free(layers);
May 6 '07 #6

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

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
5
by: Gustavo Randich | last post by:
Hello, (Using DB2/LINUX 8.2.0) HOW-TO: recreate a big set of interdependent UDFs... without having to do it manually because of the imposed drop order due to the RESTRICT keyword of the DROP...
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....
1
by: Paul-André Côté | last post by:
Hi, I've been playing with VBx and VB.NET for a while now but ... Let say in VB.Net, I've never understood how or which event I should use when I update one of two interdependent textboxes. ...
10
by: tuco357 | last post by:
This problem has been vexing me for some time and I thought I should consult the group.... Often times when writing a php script to handle some mysql DB transactions, I must write code that...
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...
4
by: Joergen Bech | last post by:
Just out of curiosity: What is your favorite method of making sure that anything that happens on a form, only happens in response to a single, external event? Take the example below. I have made...
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
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,...
1
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
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.