English 中文(简体)
班级范围
原标题:scope of classes
  • 时间:2011-10-06 14:16:11
  •  标签:
  • c++
class a
{
  private:
    b *b_obj;
  public:
    void set(int);
};

a::a()
{
  b_obj = new b;
}

a::set(int s)
{
  b_obj->c = s;
 }

class b
{
  public:
    int c;
};

is this code valid? if no, how do i make b_obj of a particular object (say a_obj) of class a ,modifiable in another class c...if a_obj i created in another class d....i am scared of a_obj going out of scope in class c.

hope you understand my question. thanks a lot for taking the time to read my post

最佳回答

该守则有效。 www.un.org/spanish/ga/president 这里是修订后的法典,还有一项试验:

class b
{
  public:
    int c;
};

class a
{
  private:
    b *b_obj;
  public:
    a();
    void set(int);
};

a::a()
{
  b_obj = new b;
}

void a::set(int s)
{
  b_obj->c = s;
}

int main()
{
    a my_a;
    my_a.set(42);
}

现在就是因为该守则是valid<>。 页: 1

  1. You don t initialize c when default-constructing b.
  2. You use raw pointers to dyanamically-allocated b s. Use automatic variables instead, whenever possible. Among the the reasons for this are ...
  3. You never delete the b you new ed in a a constructor. This results in a memory leak. If you had avoided the use of dynamic allocation in the first place, this would not be an issue.
问题回答

水井

*b_obj = *other_b_obj;// equality of objects values
b_obj = other_b_obj; // equality for pointer values

假定b_obj为物体和other_b_obj为点:

b_obj = *other_b_obj;

假设相反:

b_obj = &other_b_obj;

两者都是要点:

b_obj = other_b_obj;

对于您的最后一个问题,new对成点人并非强制性的。 点人可指出物。 然而,如果你想要指明新物体,则使用<条码>新关键词,试图制造新物体,并将新物体地址退回。

is this code valid?

该代码为几乎所有有效,但你不敢说明既定职能的返回类型:

void a::set(int s)

但是,既定职能将真正改变<代码>c > 成员<编码>b_obj。 它是公开的,因此汇编者将允许这样做。

http://ideone.com/4uDnE>rel=“nofollow> BTW,为什么没有你试图放弃吗?

除此以外,在你重新研究时,看构造者、骑士、派任经营人,并妥善执行,以释放标的<代码>b_obj。 甚至可以为此使用<条码>。

另外,我也建议你使用一个班级的“低地编码”()。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?