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

Dynamic creation of classes in c++

Randhir
Can we create a class in c++ at runtime without any idea of what it is going to contain??
Nov 25 '12 #1
6 2362
weaknessforcats
9,208 Expert Mod 8TB
No. At run time there is no C++. All you have is a binary file which could have been created written from scratch.
Nov 25 '12 #2
johny10151981
1,059 1GB
well, you can create a compiler if that what you want.............................................. ....... ;)
Nov 26 '12 #3
i actually wanted to create a program which creates datafiles at runtime. the data is supposed to be dynamic. we wont know what the datafile will contain but it will be a structure for sure. i know it is a stupid idea but i was just wondering if that is possible...
Dec 6 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
Yes it is. However, you need to format the data when you write it so that you will be able to read it back in.

For example, you could:

Expand|Select|Wrap|Line Numbers
  1. TEMP=24.5
  2. ALTiTUDE= 35000
  3. FUEL= 24500
In this scheme you read a record until you reach an =. Now look at what you read and if you see TEMP then you read until the end of the record and convert 24.5 to a double.

By having a key for each data element your data can be written in any order because you can always figure out later what was written.

Another approach is to use a CSV file (comma-separated-values). Here you need to know the order of the data items and you searate each value with a "comma". This is either a real comma or some other thing which you recognize at "not-data".

Expand|Select|Wrap|Line Numbers
  1. 24.5,35000, 24500
But you must always write temp,altitude,fuel in that order.

I would not write out actual structs but instead would unpack them and write the indiivual members. Then to read back you pack the items back into the struct. Here I would write a read and a write function where you pass in a struct pointer. This way you hide the messy details of unpacking and packing.
Dec 6 '12 #5
Banfa
9,065 Expert Mod 8TB
There are various constructs you can use in code to record the data from a file where you do not know what the precise structure of the file is before you compile you program.

Normally in these cases you would expect to know the specification of how the file will be structured, for example take weaknessforcats first example; the file contains key/value pairs. Let us simplify that example a little and say that actually all the values will be integers, so we have a file where we know every line has the following format

<KeyName> = <IntegerValue>

We do not know what the key names will actually be or the integer, values for that matter, but we can write code that will store the data. In this case a std::map<std::string, int> will happily store the data for us so we just need to write a file parser that will read the file and store the data into a map.

It is actually possible to expand this model to allow other data types as the value.

The big point here is that you may not know the exact contents of the file but you do need to know how it is structured, if you know how it is structured you can load it. There are many examples of this in existence, one of the best known being XML. XML has a strict format and because of this you can write a parser that can load any XML file and give access to the data even though throughout the world the are any 1000's of different exact file contents in XML all different. Because they all use the same structure they can all be loaded.
Dec 7 '12 #6
Thank you all. you are great teachers. i'll try and if i can't figure out i'll post again.
Dec 7 '12 #7

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

Similar topics

6
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
2
by: JC | last post by:
Hello, I'm looking for examples of how to make dynamic creation of code and dynamic execution of the same code. I want to do some code dynamically and then I want to use it to get some results....
7
by: Ollie Riches | last post by:
I know this has probably been asked a lot but I can't find any good exmaples so.... Can anyone give some url's to C# code that dynaimcally generates a web service proxy at runtime? Cheers ...
4
by: Alex | last post by:
Hi, My project will have a number of classes all implementing a single interface, e.g. class A : IA class B : IA class C : IA (etc)
1
by: Rajko | last post by:
Is it ok practice to use DECLARE_DYNCREATE with MFC classes and then to create overloaded constructor which could not be default constructor for that MFC class because there must not be more than...
1
by: dav3 | last post by:
Any help here is appreciated folks. First in my Person class the comments = errors visual basics is giving me and I am not sure why. Also when i try and set up my array of pointers to Student...
2
by: jmgopi | last post by:
Hi: Can somebody provide me samples on how to create a dynamic CollapsiblePanel using ASP.NET AJAX Toolkit. Any points are highly appreciated. Thanks, GJM
0
by: Steven W. Orr | last post by:
I have a base class B and a derived class D which inherits from B. I also have a D2 class which inherits from D. D is used as a base class for most of my generated classes. I have a "special" class...
1
by: Steven W. Orr | last post by:
Sorry, I had a small description problem. It's corrected below. I have a base class B and a derived class D which inherits from B. I also have a D2 class which inherits from D. D is used as a...
0
by: tina2626 | last post by:
I m using this code in C#.net, for dynamic creation of GridView without using DB. <CODE> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) {...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.