由买买提看人间百态

topics

全部话题 - 话题: myclass
1 2 3 下页 末页 (共3页)
X****r
发帖数: 3557
1
Same for non-POD class, e.g. if myclass has defined a constructor.
But they are different for POD class,
e.g. struct myclass {int a; int b;};
new myclass creates an object with indeterminate values, while
new myclass() creates an object zero-initialized.
t****t
发帖数: 6806
2
你吹牛
除非myClass是个macro,
否则任何情况下, const myClass和myClass const都是一样的
r*****e
发帖数: 792
3
在函数传递参数时const myClass &src 和myClass const &src有什么区别?
上网查了查,没看到和class obj相关的const的文章。
谢了
t*****g
发帖数: 1275
4
It's a good practice to put const behind the variable it applies to.
In you case, if myClass is a simple class or basic datatype there is no
difference. However, if myClass is a datetype defined as a pointer, or, a
template which could potentially become a pointer, you will encounter
unexpected errors.
d********t
发帖数: 9628
5
请问
p = new myclass

p = new myclass()
有何区别?
谢谢!
t*****g
发帖数: 1275
6
哈哈,这有啥吹牛的?
你不妨自己试一下把myclass定义成template,然后用个pointer来代替。用typedef当
然也可以。
d********t
发帖数: 9628
7

1. 啥叫POD class?
2. 如何是
class myclass
{
int a;
int b;
}
呢?
v******y
发帖数: 84
8
来自主题: Programming版 - 前几天有人问rvalue reference的
这是我的理解,不对的请指出,谢谢
The usage of rvalue reference &&
Define a class
class MyClass{
public:
MyClass(){/*require for const MyClass cMyClass init. Without this default
constructor, const MyClass will have to be declared and defined as such:
const MyClass cMyClass=MyClass(). MyClass() here will call synthesized
constructor automatically generated by compiler. This synthesized
constructor can not directly initiate const. So following is illegal without
default constructor: const MyClass cMyClass;*/}
};... 阅读全帖
m*********a
发帖数: 3299
9
来自主题: Programming版 - 请教C++11的rvalue ref
如果有MyClass
foo(MyClass())就会调用foo(MyClass&&)
MyClass()产生一个新的unamed object
如果先定义一个
MyClass myClass;
foo(myClass)就是调用foo(MyClass&)或者foo(const MyClass &)
foo(MyClass&)和foo(const MyClass &)虽然signature 不同
但是不能overload.
虽然const这儿是low level modifier, 但是const low level可以接受nonconst 和
const的variable
b***i
发帖数: 3043
10
来自主题: Programming版 - C++怎样设置全局变量
简单程序可以直接extern,也可以包在namespace里面。
很多情况下你并不需要绝对的全局变量,而只是想获取其他程序里面的值。你可以把
function变成一个class里面的function,就可以在任何地方访问这个函数。class里面
可以用static变量。
myclass.h
class myclass{
public: static updateValue(){value = myFun()};
static int value;
whatever myFun(){};//static, private, it can even be moved outside
myclass
};
myclass.cpp
int myclass::value=0;
main里面
#include "myclass.h"
myclass::updateValue()
其他cpp
#include "myclass.h"
直接用myclass::value
myclass中的value就相当于一个全局变量。你的程序复杂了之后,很多function都会在
... 阅读全帖
J**********y
发帖数: 1891
11
有几个destructor呢?
我怎么老是数不对?谢谢。
// mytry1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
using namespace std;
class myclass
{
public:
int num;
myclass(int a)
{
num=a;
}
myclass(const myclass & a)
{
num=a.num;
cout << "copy constructor ... " << endl;
}

~myclass()
{
cout << "destructor ... " << endl;

}
};
int _tmain(int argc, _TCHAR* argv[])
{
myclass x(5);
... 阅读全帖
k*****y
发帖数: 744
12
来自主题: Programming版 - 问个local class的问题
void TestFunc(){
class MyClass{
public:
Myclass(int){};
...
};
vector data;
data.push_back(MyClass(10));
...
};
这样写的话,好像copy constructor不能把10拷到data[0]去。
class MyClass{
public:
Myclass(int){};
...
};
void TestFunc(){
vector data;
data.push_back(MyClass(10));
...
};
如果把MyClass定义到全局,就能把10拷到data[0]去了。
请问这是什么原因?用的是VS2010。谢谢~
c******s
发帖数: 270
13
要被继承的类(DllObject)定义在Project A, 用DLL导出. 而子类(MyClass)定义在
Project B.
在Project A中, 我定义了宏MYDLL_BUILD.
// Config_Project_A.h
#if !defined(MYDLL_BUILD)
# pragma comment(lib, "myDll.lib")
#endif
#if defined(MYDLL_BUILD)
# define MYDLL_API __declspec(dllexport)
#else
# define MYDLL_API __declspec(dllimport)
#endif
// DllObject.h
class MYDLL_API DllObject
{
public:
virtual ~DllObject() {}
protected:
DllObject() { m_count = 3; }
priv... 阅读全帖
c**z
发帖数: 669
14
来自主题: JobHunting版 - Code for Singleton design pattern
not sure where is wrong? Can someone help?
Thanks
class myclass
{
private:
myclass() {}
static myclass uniqueinstance;
static bool b;
public:
static myclass getinstance()
{
if(b==false)
{uniqueinstance=myclass();
b=true;
}
return uniqueinstance;
}
};
bool myclass::b=false;
void main()
{
myclass.getinstance();
}
N***r
发帖数: 2539
15
来自主题: Programming版 - c++ 宏的问题
有这么一段代码
template
void foo(SomeKindofClass & myClass);
函数foo的具体实现分布在各个C文件中,比如在myProblemClassABC中
templet <>
void
foo(SomeKindofClass & myClass)
{
/* real codes here */
}
在myProblemClassXYZ中
templet <>
void
foo(SomeKindofClass & myClass)
{
/* real codes here */
}
现在有另一个function,功能是根据从输入文件中读到的myProblemClassXXX来调用foo
,很笨的方法就是这样
void function(std::vector class_names_from_input)
{
SomeKindofClass myClass = SomeKindofClass(... 阅读全帖
m**c
发帖数: 90
16
来自主题: Java版 - basic java question

No (I assume that the first line you mentioned is "private static myclass
instance = new myclass();"). The class loader will load your class before any
thread can access it.
Should "public myclass getInstance()" be "public static myclass getInstace()"?
If not, no one can create myclass since it only has private constructor.
In your case, probably not. Sometimes, people also use "Lazy Load" in their
singleton pattern, in that case, you may need to do it:
public myclass {
private static myc
c******n
发帖数: 4965
17
来自主题: Java版 - jmock ?? or any other mocking tools?
the nasty thing about jmock is that whenever I want to mock a dependent
object, I have to abstract out the object creation,
and turn a statment like
MyClass obj = new MyClass();
into
MyClass obj = createMyClass();
while production code has
MyClass createMyClass() { return new MyClass();}
and mock code has
MyClass createMyClass() { return instance_var_mock_myclass;}
I end up having a lot of createXXX() methods in each class implemented,
furthermore, clover would complain that the createXXX() them
c*****t
发帖数: 1879
18
来自主题: Java版 - 面试的一些题目
sigh, singleton lazy initiation method is this
public class MyClass
{
private final static class MyClassSingletonHelper
{
final static MyClass s_instance = new MyClass ();
}
public static MyClass getInstance ()
{
return MyClassSingletonHelper.s_instance;
}
private MyClass () { }
...
}
Note, MyClassSingletonHelper is not loaded when MyClass is loaded.
The helper class is loaded only when its member is used.
m*********a
发帖数: 3299
19
简单的返回一个right hand value?
所以不能支持(a=b)=c:
MyClass MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
}
返回一个reference,可以用 left hand value, 支持(a=b)=c:
MyClass & MyClass::operator=(const MyClass &other){
if (this!=&other)
value=other.value;
return *this;
}
m*********a
发帖数: 3299
20
来自主题: Programming版 - C++糟粕和需要避免的。
这句没有看懂
----------------------
传入的话,用const &,需要修改传入的话,用&,传出不是用return的copy constructor?
比如MyClass myClass;//declare on local stack
//do some thing with myClass
return myClass;
//myClass is copied and local myClass is out of scope and destroyed.
s****u
发帖数: 375
21
来自主题: JobHunting版 - [请教] C++ coding question
在类的定义文件myClass.cpp里,有一个non-member静态函数。有什么办法能保证这个
静态函数只被该类型的(无数多个)instances调用最多一次?
比如下面这个例子就不满足以上要求, 因为结果counter大于1.
static int counter = 0;
static void nonMemberFunction()
{
counter += 1;
}
class myClass
{
public:
myClass() { nonMemberFunction(); }
};
int main()
{
myClass A;
myClass B;
std::cout< return 0;
}
w******t
发帖数: 1422
22
来自主题: Java版 - basic java question
actually my question comes from the singleton pattern I am using.
in my singleton class (pretty standard),
public class myclass {
private static myclass instance = new myclass();
... other variables ...
private myclass() {
....
}
public myclass getInstance() {
}
...
}
question is that: is it possible first line get executed more than once??
and should we use sync key word within constructor?
I use singleton many times and never think about it carefully, but suddenly I
have thi
q*********u
发帖数: 280
23
来自主题: Java版 - 面试的一些题目
恩,
lazy的好处容易讲,
不过原帖里面讲做为drawback, 需要多添加更多的synchronized block, 其实就算
不是lazy, 需要syn的时候,还是得一样外加syn block
我理解是如果有许多singlton需要在强调performance 的地方被第一次调用出来,这样
的话,应该是增加程序局部关键处的开销。

sigh, singleton lazy initiation method is this
public class MyClass
{
private final static class MyClassSingletonHelper
{
final static MyClass s_instance = new MyClass ();
}
public static MyClass getInstance ()
{
return MyClassSingletonHelper.s_instance;
}
private MyClass () { }
.
z****e
发帖数: 54598
24
ft
还是先找本java的教材看看吧
这种问题不会的话,是基本概念问题
前几章就会说
public class MyClass{
public boolean xyzThere(String str){...}
public static void main(String[] args){
MyClass myClass = new MyClass();
myClass.xyzThere("This is parameter");
}
}
d******e
发帖数: 194
25
来自主题: Programming版 - [菜鸟问题]类模板问题
template
class MyClass
{
public:
class iterator
{
iterator operator ++ (int);
};
private:
....
};
// -- implementation
template
// -- next line got error message
MyClass::iterator MyClass::iterator::operator ++ (int)
{
iterator iter(*this);
........
return iter;
}
这个实现部分的函数头哪里有问题?我得到的g++错误信息:
error: expected constructor, destructor, or type conversion before "MyClass"
error: expected `;' before "MyClass"
s***1
发帖数: 49
26
来自主题: Programming版 - C++ 什么时候用 "new" ?
如果我有一个 class MyClass, 有一个constructor MyClass(int x).
在main 里面:
1) MyClass myObj(12);
2) MyClass myObj = new MyClass(12);
有什么区别? 哪一个更好?
s***1
发帖数: 49
27
来自主题: Programming版 - C++ 什么时候用 "new" ?
Yea, I meant to say MyClass *myObj = new MyClass(12);
I realized the difference, stack VS heap.
If I declare:
MyClass *myObj = new MyClass(12);
I can access a member function by: myObj->myFunction().
But if I declare
MyClass myObj (12);
What is the syntax for accessing the member function? Is it still myObj->
MyFunction() ?
m*********a
发帖数: 3299
28
在MyClass & operator=(const MyClass & other)中
swap(other);//copy other
return *this;
在MyClass 做swap other
class MyClass{
void swap(MyClass &other){
std::swap(this->value,other.value);
}
}
m*********a
发帖数: 3299
29
来自主题: Programming版 - 今天给c++震惊了
想到一点是不是这样的
class MyClass{
public:
int val;
};
MyClass myClass;
MyClass *p=&myClass;
p->val=100;//legal, p-val return an lvalue
(*p).val=100;//(*p).val这儿也不是an rvlaue,而是an lvalue
a****s
发帖数: 524
30
I thought it for a while because it looks interesting.
Although I didn't work through to the final solution, I am pretty sure this
can be solved in compile time by using templates
The idea is creating template functions for all possible number of arguments
a function can have, the limit I believe is 256.
For exmaple: assuming all functions return void.
typedef void(MyClass::*funcany)(...);
int count ( (void (MyClass::*func0)()) fp)
{
if (!static_cast(fp))
return -1;
return ... 阅读全帖
h*****g
发帖数: 944
31
来自主题: JobHunting版 - 再问一个碰到的C++问题
Q1) Which of the following general functions gives a pointer to the
immediate
base class of a derived class, i.e. the direct parent class?
A) parent()
B) super()
C) base()
D) This function does not exist.
我好像选了super(), 这个java里有,c++里有没有啊?
Q2) What is the problem with the following code snippet?
class myclass;
myclass* pmc = new myclass();
free(pmc);
A) The problem is that releasing memory with "free" is possible only if the
memory was allocated with "malloc".
B) The problem is that "free" ... 阅读全帖
z****e
发帖数: 54598
32
来自主题: Java版 - 关于singleton
问你跟其它几个牛人一个问题
这里锁的是(this)之类的对象么?还是(MyClass.class)?还是两个都可以?
我看你们这里都用了this或者是mylock,小写开头,我假设就是对象了
但我觉得是后者才对,也就是synchronized(MyClass.class)才对
如果锁的是this的话,那其它的MyClass类一样可以实例化很多对象出来
这里可能不会出现争抢的情况,锁this是没有太大意义的
除非assume外面这个MyClass已经是singleton了,那这个显然是有问题的
要把整个Class在方法区中锁住才行
我隐隐约约觉得不存在有锁this的singleton实现
H****r
发帖数: 2801
33
来自主题: Programming版 - 再一个问题c++
In textbook or C++ tutorial you'll find examples like:
// Define operator += ... ...
// Add this instance's value to other, and return a new instance
// with the result.
const MyClass MyClass::operator+(const MyClass &other) const
{
MyClass result = *this; // Make a copy of myself.
result += other; // Use += to add other to the copy.
return result; // All done!
}
m********r
发帖数: 334
34
来自主题: Programming版 - 问一个C++ String的初始化问题
在一个类中声明如下
class Myclass : public Object
{
public:
otherClass *A;
...
}
otherClass:otherClass(String name)
{
setName("This is " + name);
}
Myclass:Myclass
{
A = new otherClass(Str1);
}
以上没有问题,但是在Myclass的一个成员函数里如果调用
A->setName("Now it is " + "some string" );
gcc报错说 invalid operands of types const char [xx] and unsigned char [xx]
to binary operator+
为什么在构造函数里可以,这里就不行?
c*******9
发帖数: 6411
35
来自主题: Programming版 - one template question
are those two the same thing? one with after MyClass and another does
not have it..
class MyClass {
template
class MyClass
{
...
}
and
template
class MyClass
{..
}
d****i
发帖数: 4809
36
来自主题: Programming版 - 问个copy constructor的问题
是我搞糊涂了,不过非常不喜欢这种创建对象的写法,如果是stack variable,写成
MyClass a;
MyClass a(10, 1);
不就成了吗?写成MyClass a = MyClass()有点脱裤子放屁,多此一举。而且C++的code
style应该尽量保持和C一样,第二种写法非常不好,C里面stack变量不就是
typedef struct {
...
} MyStruct;
MyStruct a;
C********e
发帖数: 219
37
来自主题: Programming版 - 求助调试c++程序
接手了一个c++的程序,以前的程序员不愿意指导,自己也比较愚钝。请大侠们指点
不明白为什么usleep(1000)发生overflow?是因为其他线程有overflow的情况吗?如何
查看其他线程里面的变量值呢?
Program terminated with signal 6, Aborted.
#0 0x0000003cfd89a1e1 in nanosleep () from /lib64/libc.so.6
(gdb) bt
#0 0x0000003cfd89a1e1 in nanosleep () from /lib64/libc.so.6
#1 0x0000003cfd8ce8f4 in usleep () from /lib64/libc.so.6
#2 0x0000000000498e17 in MyClass::StartUp (this=0x2b4484725010, aName=
Traceback (most recent call last):
File "/usr/share/gdb/python/libstdcxx/v6/printers.py", lin... 阅读全帖
r***o
发帖数: 24
38
put all private data and functions into a private data blob class.
for example;
====== .h =====
class MyPrivateClass
class MyClass {
public:
// public data fields and functions
void foobar();
private:
MyPrivateClass * pc;
char datablob[xxx]; // if you don't want dynamic allocation
}
============ .cpp ========
class MyPrivateClass {
friend class MyClass;
private:
// anything you don't want to export
// all data are private, except
// MyClass is friend

}
vo... 阅读全帖
t****e
发帖数: 69
39
来自主题: Computation版 - 问个面试问题,请教
Note that the new constructor is "added," not to "replace" the default one.
For example, previously you have
MyClass::MyClass()
now you added a new constructor
MyClass::MyClass( int )
This is a new feature. Your application, however, still uses the old default
one. So your application will behave the same way whether or not using the
new library.
M**8
发帖数: 25
40
来自主题: Computation版 - 问个面试问题,请教
How about changing MyClass from
class MyClass
{
public:
int x;
};
to
class MyClass
{
public:
MyClass() {x = 10; }
int x;
};
Do we need to recompile? Or put this question in a different way, can you
give an example that making a dtor virtual will trigger application to
recompile?
M**8
发帖数: 25
41
来自主题: Computation版 - 问个面试问题,请教
How about changing MyClass from
class MyClass
{
public:
int x;
};
to
class MyClass
{
public:
MyClass() {x = 10; }
int x;
};
Do we need to recompile? Or put this question in a different way, can you
give an example that making a dtor virtual will trigger application to
recompile?
d****g
发帖数: 33
42
来自主题: JobHunting版 - 发个C++语法问题,调节一下气氛
class myclass{
private:
int m;
public:
myclass(myclass & rhs){ m = rhs.m;}
};
拷贝函数中,m是私有的,为什么还能access rhs的私有成员m.
l*********8
发帖数: 4642
43
来自主题: JobHunting版 - 这个拷贝构造函数有什么问题?
不太理解。
copy constructor是用在下面两种情况:
MyClass A(B); // B is a MyClass object
MyClass A = B;
为什么要判断参数是否就是自身呢?为什么要return呢?
你说的是operator = ?
k***s
发帖数: 6
44
来自主题: JobHunting版 - walmart Lab question
抛砖
class MyClass(object):
def __count_jumps_1(self, remaining_steps, memory):
if memory[remaining_steps] is None:
count = 0
for jump in (1, 2, 3):
if remaining_steps >= jump:
count += self.__count_jumps_1(remaining_steps - jump,
memory)
else:
break
memory[remaining_steps] = count
return memory[remaining_steps]

def count_jumps_1(self):
memory = [None] * 6... 阅读全帖
d******e
发帖数: 194
45
来自主题: Programming版 - [菜鸟问题]类模板问题
a more clear version as following:
template
class MyClass
{
public:
class Inner
{
public:
Inner(int v = 0): value(v) {};
private:
int value;
};

Inner CreateInner(int v = 0) const;
private:
T TValue;

};
template
MyClass::Inner MyClass::CreateInner(int v) const // error
{
return Inner(v);
}
It seems the problem is in return type, because it has no problem if I
implement it as inline:
Inner CreateInner(int v = 0) const
d******e
发帖数: 194
46
来自主题: Programming版 - [菜鸟问题]类模板问题
Yes, it worked, with either class or typename. but I still don't quite
understand. I already defined class MyClass 和 class Inner, what else could
MyClass::Inner could be except a class? with any T, can MyClass::Inner
mean anything else?

at
so
t****t
发帖数: 6806
47
来自主题: Programming版 - Question about vector as a class member
i guess you were a java user
in c++ you don't have to initialize v by "v=vector();", or do anything
to release it.
you also don't have to write myClass a=myClass(); just write myClass a; is
enough.
h*******s
发帖数: 8454
48
来自主题: Programming版 - 一个Quant Developer的C++面试题
like this?
class MyClass {
public:
MyClass(Callback* cb)
: done(cb) {}
~MyClass() {
done->Gogogo();
}
private:
Callback* done;
};
h*****f
发帖数: 248
49
来自主题: Programming版 - 一个Quant Developer的C++面试题
I think the question is testing whether the candidate knows:
- how to declare and store function pointer
- the raii pattern (since this design is a variant of it)
- about the problem of throwing exception in destructor as brainless poitned
out.
class MyClass {
public:
MyClass(void (*cb)()) : done(cb) {}
~MyClass() {
if (done) {
try {
(*done)();
}
catch (...) {
// choice of exit, log, throw an alert to somewhere i... 阅读全帖
g*****g
发帖数: 34805
50
所有spring bean,都可以用静态的方式获得。
MyClass myClass = applicationContext.getBean(MyClass.class);
或者,需要访问spring container也可以实现BeanFactoryAware 或
ApplicationContextAware接口。
虽然没有spring native support好,但离不能用还十万八千里。
1 2 3 下页 末页 (共3页)