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

How to put the value in dynamically in the constructor of class

151 100+
Here i am creating a graph Network, it is working fine. But I am creating the Graph object by giving the parameter 40 which is (no of elements+1) in the graph.
How can i give it dynamically (should not write 40). It should take the output of function of (number_of_vertices()+1) function ,How can we do it .Please Help me out of this pblm.

Full code removed. Please 1) use code tags and 2) post the appropriate snippet of code (not full code)
Apr 9 '08 #1
3 1188
weaknessforcats
9,208 Expert Mod 8TB
Either use the initializer list or just call your function in your constructor.
Expand|Select|Wrap|Line Numbers
  1. Network::Network()
  2. {
  3.      this->verts = new Graph[(number_of_vertices()+1)];
  4. }
  5. //OR:
  6. Network::Network() : verts(new Graph[(number_of_vertices()+1)]
  7. {
  8. }
  9.  
Then put the cleanup code in your destructor.

BTW: I recommend using handles rather than pointers.
Apr 9 '08 #2
Man4ish
151 100+
Either use the initializer list or just call your function in your constructor.
Expand|Select|Wrap|Line Numbers
  1. Network::Network()
  2. {
  3.      this->verts = new Graph[(number_of_vertices()+1)];
  4. }
  5. //OR:
  6. Network::Network() : verts(new Graph[(number_of_vertices()+1)]
  7. {
  8. }
  9.  
Then put the cleanup code in your destructor.

BTW: I recommend using handles rather than pointers.
When i am putting

Molecule::Molecule() : verts(new Graph[(number_of_vertices()+1)])
{

}
instead of this Molecule::Graph g(40); IT is giving following error.
||warning: command line option "-Wmain" is valid for C/ObjC but not for C++|
/home/manish/Desktop/Structure_Mining/com/Insilico/Create_Network/Network.cpp|47|error: definition of implicitly-declared ‘Molecule::Molecule()’|
||=== Build finished: 1 errors, 1 warnings ===|
Apr 10 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Are you doing it this way:
Expand|Select|Wrap|Line Numbers
  1. int number_of_vertices()
  2. {
  3.          return 4;          //dummy
  4. }
  5. class Graph
  6. {
  7.  
  8. };
  9. class Molecule
  10. {
  11.      Graph* verts;
  12. public:
  13.      Molecule();
  14. };
  15. Molecule::Molecule() : verts(new Graph[(number_of_vertices()+1)])
  16. {
  17.  
  18. }
  19. int main()
  20. {
  21.    Molecule m;
  22. }
  23.  
This compiles and links usign Visual Studio.NET 2008.
Apr 10 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
7
by: Ron Garret | last post by:
I need to dynamically generate new types at run time. I can do this in two ways. I can use the "type" constructor, or I can generate a "class" statement as a string and feed that to the exec...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
1
by: Domer | last post by:
In my program, I load dynamically a DLL and I'm able to get a function pointer of type "int (__cdecl *)(void)" of any function in the DLL. If a DLL has a static class function, which calls the...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
7
by: Bonj | last post by:
OK I know a constructor can't have a return value but is it feasible such that I have a class whereby //in MyClass1. class MyClass MyClass1(){} ~MyClass1(){} MyClass1(int & Result) {Result =...
4
by: Andrew Backer | last post by:
Hello, I am having a problem creating a class dynamically. The class I have is a base class of another, and the parent class has the constructor (which takes one argument). The base class...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
7
by: john_smith_1221 | last post by:
Hello, I need to use the rand() function to generate a random value, I already know how to do it with srand(time(NULL)) and its "randomness" is sufficient for me, the problem is my code requires to...
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: 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?
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
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
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...

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.