class Obj
{
public:
Obj(int a){}
}
class MyContainer
{
private:
Obj m_obj[10];
public:
MyContainer(){}
}
How Can I init array m_obj in class MyContainer? (not use std::vector).
What I have to do can make these code pass complied?
----------------------------
Co.:beijing lingtu Co.
Ad.: beijing haidian
ZIP£º 100094
Tel.: 010-82825800£*8006
Mobile:
Mail£ºzh******* ***@lingtu.com
MSN: re********@hotm ail.com
comp£º http://www.lingtu.com/
lingtu online: http://www.51ditu.com/
-------------------------- 7 2083
recover napsal:
class Obj
{
public:
Obj(int a){}
}
class MyContainer
{
private:
Obj m_obj[10];
public:
MyContainer(){}
}
How Can I init array m_obj in class MyContainer? (not use std::vector).
What I have to do can make these code pass complied?
----------------------------
Co.:beijing lingtu Co.
Ad.: beijing haidian
ZIP£º 100094
Tel.: 010-82825800£*8006
Mobile:
Mail£ºzh******* ***@lingtu.com
MSN: re********@hotm ail.com
comp£º http://www.lingtu.com/
lingtu online:http://www.51ditu.com/
--------------------------
Items in array are initiated with default constructor. You can provide
some function (let's say Create) which performs same initialization as
would be in ordinary constructor. Or you can assign to every array item
new instance created with your own non-default constructor. Or you can
use std::vector.
recover wrote:
class Obj
{
public:
Obj(int a){}
}
;
>
class MyContainer
{
private:
Obj m_obj[10];
public:
MyContainer(){}
}
;
>
How Can I init array m_obj in class MyContainer? (not use std::vector).
What I have to do can make these code pass complied?
Why not std::vector? It would simplify, enhance and empower your code.
A primitive array invokes the default constructor of each element. So
if you write class Obj properly, the elements of the array will be
initialized by its def ctor.
class Obj
{
int a;
public:
Obj() : a(0) { } // def ctor
};
template< typename T, const size_t Size >
class Container
{
T m_array[Size];
public:
Container() { }
};
int main()
{
Container< Obj, 5 container;
}
With a std::vector, the above container would be dynamic (resizeable)
and you could use any ctor, not just a def ctor. In release mode, that
vector is most likely to outperform the array as well.
recover wrote:
How Can I init array m_obj in class MyContainer? (not use std::vector).
What I have to do can make these code pass complied?
You need provide default ctor, and it seems to me, that it is not
useful property of C++. It can be better, if one can init C-style array
with any concrete ctor
concrete_class ptr=new concrete_class( params)[size];
or
concrete_class ptr=new[size] concrete_class( params);
"recover" <zh**********@l ingtu.comsaid
class Obj
{
public:
Obj(int a){}
}
class MyContainer
{
private:
Obj m_obj[10];
public:
MyContainer(){}
}
"Ondra Holub" <on*********@po st.czsaid
>>Items in array are initiated with default constructor.
In some case,Obj must not have default constructor,or the default
constructor has other use.
>>You can provide some function (let's say Create) which performs same
initialization as
would be in ordinary constructor.
this may be a good idea,if I can modify the class Obj. But if I dont't have
the code of "class Obj",I import it from some lib or dll .Or the code of
"clas Obj" is borrowed from others,I havn't right change these code.
>>Or you can assign to every array item new instance created with your own
non-default constructor.
I cant't full understand this line. Can you give me some example?
>>Or you can use std::vector.
some complier doesn't support stl, may be in embeded equipment.So I can't
use std::vector.
Thanks a lot.
"Salt_Peter " <pj*****@yahoo. comwrote
>
Why not std::vector? It would simplify, enhance and empower your code.
With a std::vector, the above container would be dynamic (resizeable)
and you could use any ctor, not just a def ctor. In release mode, that
vector is most likely to outperform the array as well.
some complier doesn't support stl, may be in embeded equipment.So I can't
use std::vector.
"Grizlyk" wrote
You need provide default ctor, and it seems to me, that it is not
useful property of C++. It can be better, if one can init C-style array
with any concrete ctor
concrete_class ptr=new concrete_class( params)[size];
or
concrete_class ptr=new[size] concrete_class( params);
Yes ,I like write like this.
class Obj
{
public:
Obj(int a){}
}
class MyContainer
{
private:
//Init in here is better??Why c++ can't support this?
Obj m_obj[3]={?Obj(3), Obj(7), Obj(4)?};
public:
MyContainer(){}
}
recover wrote:
You need provide default ctor, and it seems to me, that it is not
useful property of C++. It can be better, if one can init C-style array
with any concrete ctor
concrete_class ptr=new concrete_class( params)[size];
Obj m_obj[3]={?Obj(3), Obj(7), Obj(4)?};
No, I was speaking about calling during "new[]" (after memory
allocation has been done) for each member of array concrete ctor (equal
for each member), instead of default ctor. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vij |
last post by:
I can do this
int a = { 5,6,7,8,9};
but how can I do this inside a class? something like this
class CTest
{
private:
int a // Init the array here
};
|
by: Earl Teigrob |
last post by:
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)
Anyway, How to I dyanamically create a new instance of a class from
ClassName string?
Thanks for your Help
|
by: Wylbur via DotNetMonster.com |
last post by:
Hello to all of you geniuses,
I'm having a problem trying to get an Init handler to fire for a Placeholder
control
at the initialization phase. I’ve posted this problem to 3 other ASP.NET
forums,
and noone wants to touch it.
I tried to attach a literal control to a placeholder:
<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>
|
by: axel22 |
last post by:
Hello.
The idea is to create a class MyClass which holds a container
MyContainer with pointers to other instances of MyClass:
---------------
MyClass.h
---------------
#pragma once
|
by: Gregor Horvath |
last post by:
Hi,
I want to reference a class itself in its body:
class SomeElement(object):
def __init__(self, mycontainer):
self.mycontainer=mycontainer
class SomeContainer(object):
a = SomeElement(SomeContainer)
| |
by: josh |
last post by:
Hi, which is the way to init a static array inside a constructor
function?
i.e. if I had to init ---> int x = {100,200}
|
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 value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially...
|
by: arnaudk |
last post by:
I'm trying to design a simple container class for some data of different types based on a vector of structs, but the vector and struct are protected so that the implemenation of my container class can change independently of the interface.
using namespace std;
class MyContainer {
public:
// public stuff
protected:
struct Item {
|
by: Andrew Backer |
last post by:
I hope someone out there can help me with this, because I am stuck.
My problem is that I have an instance of a generic class, but I don't know
ahead of time what the type parameter used was. In my case the type must
derive from a class, like UserControl, so I at least know I should be able
to use that.
I can not figure out a way to get this as an instance of the generic class,
but with the base class as the type parameter. I am...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |